#!/usr/bin/env bash
#===--- build-script-impl - Implementation details of build-script ---------===#
#
## This source file is part of the Swift.org open source project
##
## Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
## Licensed under Apache License v2.0 with Runtime Library Exception
##
## See https://swift.org/LICENSE.txt for license information
## See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
#
#===------------------------------------------------------------------------===#

#
# This script is an implementation detail of other build scripts and should not
# be called directly.
#
# Note: This script will NOT auto-clean before building.
#

set -o pipefail
set -e

umask 0022

# Declare the set of known settings along with each one's description
#
# If you add a user-settable variable, add it to this list.
#
# A default value of "" indicates that the corresponding variable
# will remain unset unless set explicitly.
#
# The --skip-build parameter, with no product name, does not affect the
# configuration (CMake parameters). You can turn this option on and
# off in different invocations of the script for the same build
# directory without affecting configutation.
#
# skip-build-* and build-* parameters affect the CMake configuration
# (enable/disable those components).
#
# Each variable name is re-exported into this script in uppercase, where dashes
# are substituted by underscores. For example, `swift-install-components` is
# referred to as `SWIFT_INSTALL_COMPONENTS` in the remainder of this script.

    # name                                        default           description
KNOWN_SETTINGS=(
    ## Debugging Options
    dry-run                                       ""                "print the commands that would be executed, but do not execute them"
    verbose-build                                 ""                "print the commands executed during the build"
    check-args-only                               ""                "set to check all arguments are known. Exit with status 0 if success, non zero otherwise"
    only-execute                                  "all"             "Only execute the named action (see implementation)"
    check-incremental-compilation                 "0"               "set to 1 to compile swift libraries multiple times to check if incremental compilation works"

    ## Build Mode Options
    enable-asan                                   ""                "enable Address Sanitizer"
    enable-ubsan                                  ""                "enable Undefined Behavior Sanitizer"
    clang-profile-instr-use                       ""                "If set, profile file to use for clang PGO while building llvm/clang"
    swift-profile-instr-use                       ""                "If set, profile file to use for clang PGO while building swift"

    ## Build Tool Settings
    build-args                                    ""                "arguments to the build tool; defaults to -j8 when CMake generator is \"Unix Makefiles\""
    build-dir                                     ""                "out-of-tree build directory; default is in-tree. **This argument is required**"
    build-jobs                                    ""                "The number of parallel build jobs to use"
    build-runtime-with-host-compiler              ""                "use the host c++ compiler to build everything"
    build-stdlib-deployment-targets               "all"             "space-separated list that filters which of the configured targets to build the Swift standard library for, or 'all'"
    build-toolchain-only                          ""                "If set, only build the necessary tools to build an external toolchain"
    cmake-generator                               "Unix Makefiles"  "kind of build system to generate; see output of 'cmake --help' for choices"
    llvm-num-parallel-lto-link-jobs               ""                "The number of parallel link jobs to use when compiling llvm"
    reconfigure                                   ""                "force a CMake configuration run even if CMakeCache.txt already exists"
    skip-reconfigure                              ""                "set to skip reconfigure"
    swift-tools-num-parallel-lto-link-jobs        ""                "The number of parallel link jobs to use when compiling swift tools"
    use-gold-linker                               ""                "Enable using the gold linker"
    workspace                                     "${HOME}/src"     "source directory containing llvm, clang, swift"

    ## Build Tools
    host-cc                                       ""                "the path to CC, the 'clang' compiler for the host platform. **This argument is required**"
    host-cxx                                      ""                "the path to CXX, the 'clang++' compiler for the host platform. **This argument is required**"
    host-libtool                                  ""                "the path to libtool"
    host-lipo                                     ""                "the path to lipo for creating universal binaries on Darwin"
    cmake                                         ""                "path to the cmake binary"
    distcc                                        ""                "use distcc in pump mode"
    distcc-pump                                   ""                "the path to distcc pump executable. This argument is required if distcc is set."
    ninja-bin                                     ""                "the path to Ninja tool"

    ## Android Options
    android-api-level                             ""                "The Android API level to target when building for Android. Currently only 21 or above is supported"
    android-arch                                  "armv7"           "The Android target architecture when building for Android"
    android-deploy-device-path                    ""                "Path on an Android device to which built Swift stdlib products will be deployed"
    android-icu-data                              ""                "Path to libicudata.so"
    android-icu-i18n                              ""                "Path to libicui18n.so"
    android-icu-i18n-include                      ""                "Path to a directory containing headers libicui18n"
    android-icu-uc                                ""                "Path to libicuuc.so"
    android-icu-uc-include                        ""                "Path to a directory containing headers for libicuuc"
    android-ndk                                   ""                "An absolute path to the NDK that will be used as a libc implementation for Android builds"
    android-ndk-gcc-version                       ""                "The GCC version to use when building for Android. Currently only 4.9 is supported"

    ## Darwin Options
    darwin-crash-reporter-client                  ""                "whether to enable CrashReporter integration"
    darwin-deployment-version-ios                 "7.0"             "minimum deployment target version for iOS"
    darwin-deployment-version-osx                 "10.9"            "minimum deployment target version for OS X"
    darwin-deployment-version-tvos                "9.0"             "minimum deployment target version for tvOS"
    darwin-deployment-version-watchos             "2.0"             "minimum deployment target version for watchOS"
    darwin-install-extract-symbols                ""                "whether to extract symbols with dsymutil during installations"
    darwin-install-extract-symbols-use-just-built-dsymutil "1"       "whether we should extract symbols using the just built dsymutil"
    darwin-overlay-target                         ""                "single overlay target to build, dependencies are computed later"
    darwin-sdk-deployment-targets                 "xctest-ios-8.0"  "semicolon-separated list of triples like 'fookit-ios-9.0;barkit-watchos-9.0'"
    darwin-stdlib-install-name-dir                ""                "the directory of the install_name for standard library dylibs"
    darwin-toolchain-alias                        ""                "Swift alias for toolchain"
    darwin-toolchain-application-cert             ""                "Application Cert name to codesign xctoolchain"
    darwin-toolchain-bundle-identifier            ""                "CFBundleIdentifier for xctoolchain info plist"
    darwin-toolchain-display-name                 ""                "Display Name for xctoolcain info plist"
    darwin-toolchain-display-name-short           ""                "Display Name with out date for xctoolchain info plist"
    darwin-toolchain-installer-cert               ""                "Installer Cert name to create installer pkg"
    darwin-toolchain-installer-package            ""                "The path to installer pkg"
    darwin-toolchain-name                         ""                "Directory name for xctoolchain"
    darwin-toolchain-version                      ""                "Version for xctoolchain info plist and installer pkg"
    darwin-xcrun-toolchain                        "default"         "the name of the toolchain to use on Darwin"

    ## Build Types for Components
    swift-stdlib-build-type                       "Debug"           "the CMake build variant for Swift"

    ## Skip Build ...
    skip-build                                    ""                "set to configure as usual while skipping the build step"
    skip-build-android                            ""                "set to skip building Swift stdlibs for Android"
    skip-build-benchmarks                         ""                "set to skip building Swift Benchmark Suite"
    skip-build-clang-tools-extra                  ""                "set to skip building clang-tools-extra as part of llvm"
    skip-build-compiler-rt                        ""                "set to skip building Compiler-RT"
    skip-build-lld                                ""                "set to skip building lld as part of llvm (linux only)"

    ## Skip Test ...
    skip-test-benchmarks                          ""                "set to skip running Swift Benchmark Suite"
    skip-test-sourcekit                           ""                "set to skip testing SourceKit"

    ## Extra ... CMake Options
    common-cmake-options                          ""                "CMake options used for all targets, including LLVM/Clang"
    extra-cmake-options                           ""                "Extra options to pass to CMake for all targets"
    ninja-cmake-options                           ""                "CMake options used for all ninja targets"
    ninja-cmake-options                           ""                "CMake options used for all ninja targets"

    ## Build ...
    build-llvm                                    "1"               "set to 1 to build LLVM and Clang"
    build-sil-debugging-stdlib                    "0"               "set to 1 to build the Swift standard library with -gsil to enable debugging and profiling on SIL level"
    build-swift-dynamic-sdk-overlay               ""                "set to 1 to build dynamic variants of the Swift SDK overlay"
    build-swift-dynamic-stdlib                    ""                "set to 1 to build dynamic variants of the Swift standard library"
    build-swift-examples                          "1"               "set to 1 to build examples"
    build-swift-remote-mirror                     "1"               "set to 1 to build the Swift Remote Mirror library"
    build-swift-static-sdk-overlay                ""                "set to 1 to build static variants of the Swift SDK overlay"
    build-swift-static-stdlib                     ""                "set to 1 to build static variants of the Swift standard library"
    build-swift-stdlib-unittest-extra             "0"               "set to 1 to build optional StdlibUnittest components"
    build-swift-tools                             "1"               "set to 1 to build Swift host tools"

    ## Test Options
    llvm-include-tests                            "1"               "Set to true to generate testing targets for LLVM. Set to true by default."
    long-test                                     "0"               "set to run the long test suite"
    only-executable-test                          ""                "only run the executable variant of the swift lit tests"
    stress-test                                   "0"               "set to run the stress test suite"
    stress-test-sourcekit                         ""                "set to run the stress-SourceKit target"
    swift-include-tests                           "1"               "Set to true to generate testing targets for Swift. This allows the build to proceed when 'test' directory is missing (required for B&I builds)"
    validation-test                               "0"               "set to run the validation test suite"

    ## llbuild Options
    llbuild-enable-assertions                     "1"               "enable assertions in llbuild"

    ## LLDB Options
    lldb-assertions                               "1"               "build lldb with assertions enabled"
    lldb-extra-cmake-args                         ""                "extra command line args to pass to lldb cmake"
    lldb-no-debugserver                           ""                "delete debugserver after building it, and don't try to codesign it"
    lldb-test-cc                                  ""                "CC to use for building LLDB testsuite test inferiors.  Defaults to just-built, in-tree clang.  If set to 'host-toolchain', sets it to same as host-cc."
    lldb-test-swift-compatibility                 ""                "specify additional Swift compilers to test lldb with"
    lldb-test-swift-only                          "0"               "when running lldb tests, only include Swift-specific tests"
    lldb-use-system-debugserver                   ""                "don't try to codesign debugserver, and use the system's debugserver instead"

    ## LLVM Options
    llvm-enable-lto                               ""                "Must be set to one of 'thin' or 'full'"
    llvm-enable-modules                           "0"               "enable building llvm using modules"
    llvm-install-components                       ""                "a semicolon-separated list of LLVM components to install"
    llvm-lit-args                                 ""                "If set, override the lit args passed to LLVM"
    enable-llvm-assertions                        "1"               "set to enable llvm assertions"

    ## Swift Options
    swift-analyze-code-coverage                   "not-merged"      "Code coverage analysis mode for Swift (false, not-merged, merged). Defaults to false if the argument is not present, and not-merged if the argument is present without a modifier."
    swift-enable-assertions                       "1"               "enable assertions in Swift"
    swift-enable-ast-verifier                     "1"               "If enabled, and the assertions are enabled, the built Swift compiler will run the AST verifier every time it is invoked"
    swift-install-components                      ""                "a semicolon-separated list of Swift components to install"
    swift-primary-variant-arch                    ""                "default arch for target binaries"
    swift-primary-variant-sdk                     ""                "default SDK for target binaries"
    swift-runtime-enable-leak-checker             "0"               "Enable leaks checking routines in the runtime"
    swift-stdlib-enable-assertions                "1"               "enable assertions in Swift"
    swift-stdlib-use-nonatomic-rc                 "0"               "build the Swift stdlib and overlays with nonatomic reference count operations enabled"
    swift-tools-enable-lto                        ""                "enable LTO compilation of Swift tools. *NOTE* This does not include the swift standard library and runtime. Must be set to one of 'thin' or 'full'"
    extra-swift-args                              ""                "Extra arguments to pass to swift modules which match regex. Assumed to be a flattened cmake list consisting of [module_regexp, args, module_regexp, args, ...]"
    report-statistics                             "0"               "set to 1 to generate compilation statistics files for swift libraries"
    sil-verify-all                                "0"               "If enabled, run the SIL verifier after each transform when building Swift files during this build process"
    stdlib-deployment-targets                     ""                "space-separated list of targets to configure the Swift standard library to be compiled or cross-compiled for"

    ## Uncategorised
    install-prefix                                ""                "installation prefix"
    toolchain-prefix                              ""                "the path to the .xctoolchain directory that houses the install prefix path"
    install-destdir                               ""                "the path to use as the filesystem root for the installation"
    install-symroot                               ""                "the path to install debug symbols into"
    installable-package                           ""                "the path to the archive of the installation directory"
    test-installable-package                      ""                "whether to run post-packaging tests on the produced package"
    skip-local-build                              ""                "set to skip building for the current host (useful when crosscompiling)"
    test-paths                                    ""                "run tests located in specific directories and/or files"
    native-llvm-tools-path                        ""                "directory that contains LLVM tools that are executable on the build machine"
    native-clang-tools-path                       ""                "directory that contains Clang tools that are executable on the build machine"
    native-swift-tools-path                       ""                "directory that contains Swift tools that are executable on the build machine"
    embed-bitcode-section                         "0"               "embed an LLVM bitcode section in stdlib/overlay binaries for supported platforms"
    host-target                                   ""                "The host target. LLVM, Clang, and Swift will be built for this target. The built LLVM and Clang will be used to compile Swift for the cross-compilation targets. **This argument is required**"
    cross-compile-hosts                           ""                "space-separated list of targets to cross-compile host Swift tools for"
    cross-compile-with-host-tools                 ""                "set to use the clang we build for the host to then build the cross-compile hosts"
    cross-compile-install-prefixes                ""                "semicolon-separated list of install prefixes to use for the cross-compiled hosts. The list expands, so if there are more cross-compile hosts than prefixes, unmatched hosts use the last prefix in the list"
    skip-merge-lipo-cross-compile-tools           ""                "set to skip running merge-lipo after installing cross-compiled host Swift tools"
    coverage-db                                   ""                "If set, coverage database to use when prioritizing testing"
    skip-local-host-install                       ""                "If we are cross-compiling multiple targets, skip an install pass locally if the hosts match"
)

components=(
  cmark
  foundation
  libcxx
  libdispatch
  libicu
  llbuild
  lldb
  llvm
  static-foundation
  static-libdispatch
  swift
  xctest
)
for component in ${components[@]} ; do
  KNOWN_SETTINGS+=(
    ${component}-build-type                       "Debug"           "the build variant for ${component}"
    ${component}-cmake-options                    ""                "CMake options used for ${component}"
    skip-build-${component}                       ""                "set to skip building ${component}"
    skip-test-${component}                        ""                "set to skip testing ${component}"
    install-${component}                          ""                "whether to install ${component}"
  )
done

# Centralized access point for traced command invocation.
# Every operation that might mutates file system should be called via
# these functions.

function call() {
    if [[ ${DRY_RUN} ]] || [[ "${VERBOSE_BUILD}" ]]; then
        echo "${PS4}"$(quoted_print "$@")
    fi
    if [[ ! ${DRY_RUN} ]]; then
        { set -x; } 2>/dev/null
        "$@"
        { set +x; } 2>/dev/null
    fi
}

function with_pushd() {
    local dir=$1
    shift
    if [[ "$1" == "call" ]]; then
        shift
    fi
    if [[ ${DRY_RUN} ]]; then
        echo ${PS4}pushd "${dir}"
        echo "${PS4}"$(quoted_print "$@")
        echo ${PS4}popd
    else
        set -x
        pushd "${dir}"
        "$@"
        { set -x; } 2>/dev/null # because $@ might includes { set +x; }
        popd
        { set +x; } 2>/dev/null
    fi
}

function quoted_print() {
    python -c 'import pipes; import sys; print(" ".join(pipes.quote(arg) for arg in sys.argv[1:]))' "$@"
}

function toupper() {
    echo "$@" | tr '[:lower:]' '[:upper:]'
}

function tolower() {
    echo "$@" | tr '[:upper:]' '[:lower:]'
}

function true_false() {
    case "$1" in
        false | FALSE | 0 | "")
            echo "FALSE"
            ;;
        true | TRUE | 1)
            echo "TRUE"
            ;;
        *)
            echo "true_false: unknown value: $1" >&2
            exit 1
            ;;
    esac
}

function to_varname() {
    # Uses `tr` because it is orders of magnitude faster than ${1//-/_} on long
    # strings, which happens when translating KNOWN_SETTINGS.
    toupper "$(echo $1 | tr '-' '_')"
}

function is_llvm_lto_enabled() {
    if [[ "${LLVM_ENABLE_LTO}" == "thin" ]] ||
       [[ "${LLVM_ENABLE_LTO}" == "full" ]]; then
        echo "TRUE"
    else
        echo "FALSE"
    fi
}

function is_swift_lto_enabled() {
    if [[ "${SWIFT_TOOLS_ENABLE_LTO}" == "thin" ]] ||
       [[ "${SWIFT_TOOLS_ENABLE_LTO}" == "full" ]]; then
        echo "TRUE"
    else
        echo "FALSE"
    fi
}

# Support for performing isolated actions.
#
# This is part of refactoring more work to be done or controllable via
# `build-script` itself. For additional information, see:
# https://bugs.swift.org/browse/SR-237
#
# To use this functionality, the script is invoked with:
#   ONLY_EXECUTE=<action name>
# where <action name> is one of:
#   all                          -- execute all actions
#   ${host}-${product}-build     -- the build of the product
#   ${host}-${product}-test      -- the test of the product
#   ${host}-${product}-install   -- the install of the product
#   ${host}-package              -- the package construction and test
#   merged-hosts-lipo            -- the lipo step, if used
# and if used, only the one individual action will be performed.
#
# If not set, the default is `all`.

# should_execute_action(name) -> 1 or nil
#
# Check if the named action should be run in the given script invocation.
function should_execute_action() {
    local name="$1"

    if [[ "${ONLY_EXECUTE}" = "all" ]] ||
           [[ "${ONLY_EXECUTE}" = "${name}" ]]; then
        echo 1
    fi
}

# should_execute_host_actions_for_phase(host, phase-name) -> 1 or nil
#
# Check if the there are any actions to execute for this host and phase (i.e.,
# "build", "test", or "install")
function should_execute_host_actions_for_phase() {
    local host="$1"
    local phase_name="$2"

    if [[ "${ONLY_EXECUTE}" = "all" ]] ||
           [[ "${ONLY_EXECUTE}" == ${host}-*-${phase_name} ]]; then
        echo 1
    fi
}

function verify_host_is_supported() {
    local host="$1"
    case ${host} in
      freebsd-x86_64            \
      | openbsd-amd64           \
      | cygwin-x86_64           \
      | haiku-x86_64            \
      | linux-x86_64            \
      | linux-i686              \
      | linux-armv6             \
      | linux-armv7             \
      | linux-aarch64           \
      | linux-powerpc64         \
      | linux-powerpc64le       \
      | linux-s390x             \
      | macosx-x86_64           \
      | macosx-arm64            \
      | macosx-arm64e           \
      | iphonesimulator-i386    \
      | iphonesimulator-x86_64  \
      | iphonesimulator-arm64   \
      | iphoneos-armv7          \
      | iphoneos-armv7s         \
      | iphoneos-arm64          \
      | iphoneos-arm64e         \
      | appletvsimulator-x86_64 \
      | appletvsimulator-arm64  \
      | appletvos-arm64         \
      | watchsimulator-i386     \
      | watchsimulator-arm64    \
      | watchos-armv7k          \
      | android-armv7           \
      | android-aarch64)
        ;;
      *)
        echo "Unknown host tools target: ${host}"
        exit 1
        ;;
    esac
}

function set_build_options_for_host() {
    llvm_cmake_options=()
    swift_cmake_options=()
    cmark_cmake_options=()
    lldb_cmake_options=()
    SWIFT_HOST_VARIANT=
    SWIFT_HOST_VARIANT_SDK=
    SWIFT_HOST_VARIANT_ARCH=
    SWIFT_HOST_TRIPLE=
    local host="$1"

    # Hosts which can be cross-compiled must specify:
    # SWIFT_HOST_TRIPLE and llvm_target_arch.
    # Hosts which have differing platform names from their
    # SWIFT_HOST_VARIANT_* values should change them here as well.

    verify_host_is_supported $host

    local platform=${host%%-*}
    local architecture=${host##*-}

    SWIFT_HOST_VARIANT=$platform
    SWIFT_HOST_VARIANT_SDK=$(toupper $platform)
    SWIFT_HOST_VARIANT_ARCH=$architecture

    case ${host} in
        android-aarch64)
            SWIFT_HOST_TRIPLE="aarch64-unknown-linux-android"
            llvm_target_arch="AArch64"
            ;;
        linux-armv6)
            SWIFT_HOST_TRIPLE="armv6-unknown-linux-gnueabihf"
            llvm_target_arch="ARM"
            ;;
        linux-armv7)
            SWIFT_HOST_TRIPLE="armv7-unknown-linux-gnueabihf"
            llvm_target_arch="ARM"
            ;;
        macosx-*            | \
        iphoneos-*          | \
        iphonesimulator-*   | \
        appletvos-*         | \
        appletvsimulator-*  | \
        watchos-*           | \
        watchsimulator-*)
            swift_cmake_options+=(
              -DPython2_EXECUTABLE=$(xcrun -f python2.7)
              -DPython3_EXECUTABLE=$(xcrun -f python3)
            )
            case ${host} in
                macosx-x86_64)
                    SWIFT_HOST_TRIPLE="x86_64-apple-macosx${DARWIN_DEPLOYMENT_VERSION_OSX}"
                    llvm_target_arch=""

                    SWIFT_HOST_VARIANT_SDK="OSX"
                    cmake_osx_deployment_target="${DARWIN_DEPLOYMENT_VERSION_OSX}"
                    ;;
                macosx-arm64)
                    xcrun_sdk_name="macosx"
                    llvm_target_arch="AArch64"
                    SWIFT_HOST_TRIPLE="arm64-apple-macosx${DARWIN_DEPLOYMENT_VERSION_OSX}"
                    SWIFT_HOST_VARIANT="macosx"
                    SWIFT_HOST_VARIANT_SDK="OSX"
                    SWIFT_HOST_VARIANT_ARCH="arm64"
                    cmake_osx_deployment_target="${DARWIN_DEPLOYMENT_VERSION_OSX}"
                    ;;
                macosx-arm64e)
                    xcrun_sdk_name="macosx"
                    llvm_target_arch="AArch64"
                    SWIFT_HOST_TRIPLE="arm64e-apple-macosx${DARWIN_DEPLOYMENT_VERSION_OSX}"
                    SWIFT_HOST_VARIANT="macosx"
                    SWIFT_HOST_VARIANT_SDK="OSX"
                    SWIFT_HOST_VARIANT_ARCH="arm64e"
                    cmake_osx_deployment_target="${DARWIN_DEPLOYMENT_VERSION_OSX}"
                    ;;
                iphonesimulator-i386)
                    SWIFT_HOST_TRIPLE="i386-apple-ios${DARWIN_DEPLOYMENT_VERSION_IOS}-simulator"
                    llvm_target_arch="X86"

                    SWIFT_HOST_VARIANT_SDK="IOS_SIMULATOR"
                    cmake_osx_deployment_target=""
                    ;;
                iphonesimulator-x86_64)
                    SWIFT_HOST_TRIPLE="x86_64-apple-ios${DARWIN_DEPLOYMENT_VERSION_IOS}-simulator"
                    llvm_target_arch="X86"

                    SWIFT_HOST_VARIANT_SDK="IOS_SIMULATOR"
                    cmake_osx_deployment_target=""
                    ;;
                iphonesimulator-arm64)
                    xcrun_sdk_name="iphonesimulator"
                    llvm_target_arch="AArch64"
                    SWIFT_HOST_TRIPLE="arm64-apple-ios${DARWIN_DEPLOYMENT_VERSION_IOS}-simulator"
                    SWIFT_HOST_VARIANT="iphonesimulator"
                    SWIFT_HOST_VARIANT_SDK="IOS_SIMULATOR"
                    SWIFT_HOST_VARIANT_ARCH="arm64"

                    cmake_osx_deployment_target=""
                    cmark_cmake_options=(
                        -DCMAKE_C_FLAGS="$(cmark_c_flags ${host})"
                        -DCMAKE_CXX_FLAGS="$(cmark_c_flags ${host})"
                        -DCMAKE_OSX_SYSROOT:PATH="$(xcrun --sdk ${xcrun_sdk_name} --show-sdk-path)"
                    )
                    ;;
                iphoneos-armv7)
                    SWIFT_HOST_TRIPLE="armv7-apple-ios${DARWIN_DEPLOYMENT_VERSION_IOS}"
                    llvm_target_arch="ARM"

                    SWIFT_HOST_VARIANT_SDK="IOS"
                    cmake_osx_deployment_target=""
                    ;;
                iphoneos-armv7s)
                    SWIFT_HOST_TRIPLE="armv7s-apple-ios${DARWIN_DEPLOYMENT_VERSION_IOS}"
                    llvm_target_arch="ARM"

                    SWIFT_HOST_VARIANT_SDK="IOS"
                    cmake_osx_deployment_target=""
                    ;;
                iphoneos-arm64)
                    SWIFT_HOST_TRIPLE="arm64-apple-ios${DARWIN_DEPLOYMENT_VERSION_IOS}"
                    llvm_target_arch="AArch64"

                    SWIFT_HOST_VARIANT_SDK="IOS"
                    cmake_osx_deployment_target=""
                    ;;
                iphoneos-arm64e)
                    SWIFT_HOST_TRIPLE="arm64e-apple-ios${DARWIN_DEPLOYMENT_VERSION_IOS}"
                    llvm_target_arch="AArch64"

                    SWIFT_HOST_VARIANT_SDK="IOS"
                    cmake_osx_deployment_target=""
                    ;;
                appletvsimulator-x86_64)
                    SWIFT_HOST_TRIPLE="x86_64-apple-tvos${DARWIN_DEPLOYMENT_VERSION_TVOS}-simulator"
                    llvm_target_arch="X86"

                    SWIFT_HOST_VARIANT_SDK="TVOS_SIMULATOR"
                    cmake_osx_deployment_target=""
                    ;;
                appletvsimulator-arm64)
                    xcrun_sdk_name="appletvsimulator"
                    llvm_target_arch="AArch64"
                    SWIFT_HOST_TRIPLE="arm64-apple-tvos${DARWIN_DEPLOYMENT_VERSION_IOS}-simulator"
                    SWIFT_HOST_VARIANT="appletvsimulator"
                    SWIFT_HOST_VARIANT_SDK="TVOS_SIMULATOR"
                    SWIFT_HOST_VARIANT_ARCH="arm64"

                    cmake_osx_deployment_target=""
                    cmark_cmake_options=(
                        -DCMAKE_C_FLAGS="$(cmark_c_flags ${host})"
                        -DCMAKE_CXX_FLAGS="$(cmark_c_flags ${host})"
                        -DCMAKE_OSX_SYSROOT:PATH="$(xcrun --sdk ${xcrun_sdk_name} --show-sdk-path)"
                    )
                    ;;
                appletvos-arm64)
                    SWIFT_HOST_TRIPLE="arm64-apple-tvos${DARWIN_DEPLOYMENT_VERSION_TVOS}"
                    llvm_target_arch="AArch64"

                    SWIFT_HOST_VARIANT_SDK="TVOS"
                    cmake_osx_deployment_target=""
                    ;;
                watchsimulator-i386)
                    SWIFT_HOST_TRIPLE="i386-apple-watchos${DARWIN_DEPLOYMENT_VERSION_WATCHOS}-simulator"
                    llvm_target_arch="X86"

                    SWIFT_HOST_VARIANT_SDK="WATCHOS_SIMULATOR"
                    cmake_osx_deployment_target=""
                    ;;
                watchsimulator-arm64)
                    xcrun_sdk_name="watchsimulator"
                    llvm_target_arch="AArch64"
                    SWIFT_HOST_TRIPLE="arm64-apple-watchos${DARWIN_DEPLOYMENT_VERSION_IOS}-simulator"
                    SWIFT_HOST_VARIANT="watchsimulator"
                    SWIFT_HOST_VARIANT_SDK="WATCHOS_SIMULATOR"
                    SWIFT_HOST_VARIANT_ARCH="arm64"

                    cmake_osx_deployment_target=""
                    cmark_cmake_options=(
                        -DCMAKE_C_FLAGS="$(cmark_c_flags ${host})"
                        -DCMAKE_CXX_FLAGS="$(cmark_c_flags ${host})"
                        -DCMAKE_OSX_SYSROOT:PATH="$(xcrun --sdk ${xcrun_sdk_name} --show-sdk-path)"
                    )
                    ;;
                watchos-armv7k)
                    SWIFT_HOST_TRIPLE="armv7k-apple-watchos${DARWIN_DEPLOYMENT_VERSION_WATCHOS}"
                    llvm_target_arch="ARM"

                    SWIFT_HOST_VARIANT_SDK="WATCHOS"
                    cmake_osx_deployment_target=""
                    ;;
            esac

            if [[ "${DARWIN_SDK_DEPLOYMENT_TARGETS}" != "" ]]; then
                # IFS is immediately unset after its use to avoid unwanted
                # replacement of characters in subsequent lines.
                local IFS=";"; DARWIN_SDK_DEPLOYMENT_TARGETS=($DARWIN_SDK_DEPLOYMENT_TARGETS); unset IFS

                for target in "${DARWIN_SDK_DEPLOYMENT_TARGETS[@]}"; do
                    # IFS is immediately unset after its use to avoid unwanted
                    # replacement of characters in subsequent lines.
                    local IFS="-"; triple=($target); unset IFS
                    sdk_target=$(toupper ${triple[0]}_${triple[1]})
                    swift_cmake_options+=(
                        "-DSWIFTLIB_DEPLOYMENT_VERSION_${sdk_target}=${triple[2]}"
                    )
                done
            fi

            cmake_os_sysroot="$(xcrun --sdk ${platform} --show-sdk-path)"

            # Customize the CMake `find_*` search paths to search the SDK
            # sysroot before searching `pkg-config` paths. This prevents compile
            # failures caused when `pkg-config` returns CommandLineTools paths
            # instead of Xcode paths.
            #
            # These search paths can be user customized either by setting the
            # `CMAKE_PREFIX_PATH` environment variable, or by setting the same
            # CMake variable via the command line (`-DCMAKE_PREFIX_PATH`).
            # See https://cmake.org/cmake/help/latest/command/find_path.html
            export CMAKE_PREFIX_PATH="${CMAKE_PREFIX_PATH}:${cmake_os_sysroot}/usr"

            cmark_cmake_options=(
                -DCMAKE_C_FLAGS="$(cmark_c_flags ${host})"
                -DCMAKE_CXX_FLAGS="$(cmark_c_flags ${host})"
                -DCMAKE_OSX_SYSROOT:PATH="${cmake_os_sysroot}"
                -DCMAKE_OSX_DEPLOYMENT_TARGET="${cmake_osx_deployment_target}"
            )
            llvm_cmake_options=(
                -DCMAKE_OSX_DEPLOYMENT_TARGET:STRING="${cmake_osx_deployment_target}"
                -DCMAKE_OSX_SYSROOT:PATH="${cmake_os_sysroot}"
                -DCOMPILER_RT_ENABLE_IOS:BOOL=FALSE
                -DCOMPILER_RT_ENABLE_WATCHOS:BOOL=FALSE
                -DCOMPILER_RT_ENABLE_TVOS:BOOL=FALSE
                -DSANITIZER_MIN_OSX_VERSION="${cmake_osx_deployment_target}"
                -DLLVM_ENABLE_MODULES:BOOL="$(true_false ${LLVM_ENABLE_MODULES})"
            )
            if [[ $(is_llvm_lto_enabled) == "TRUE" ]]; then
                llvm_cmake_options+=(
                    "-DLLVM_PARALLEL_LINK_JOBS=${LLVM_NUM_PARALLEL_LTO_LINK_JOBS}"
                )
            fi

            if [[ $(is_swift_lto_enabled) == "TRUE" ]]; then
                llvm_cmake_options+=(
                    -DLLVM_ENABLE_MODULE_DEBUGGING:BOOL=NO
                )

                swift_cmake_options+=(
                    -DLLVM_ENABLE_MODULE_DEBUGGING:BOOL=NO
                    "-DSWIFT_PARALLEL_LINK_JOBS=${SWIFT_TOOLS_NUM_PARALLEL_LTO_LINK_JOBS}"
                )
            fi

            swift_cmake_options+=(
                -DSWIFT_DARWIN_DEPLOYMENT_VERSION_OSX="${DARWIN_DEPLOYMENT_VERSION_OSX}"
                -DSWIFT_DARWIN_DEPLOYMENT_VERSION_IOS="${DARWIN_DEPLOYMENT_VERSION_IOS}"
                -DSWIFT_DARWIN_DEPLOYMENT_VERSION_TVOS="${DARWIN_DEPLOYMENT_VERSION_TVOS}"
                -DSWIFT_DARWIN_DEPLOYMENT_VERSION_WATCHOS="${DARWIN_DEPLOYMENT_VERSION_WATCHOS}"
                -DCMAKE_OSX_SYSROOT:PATH="${cmake_os_sysroot}"
                # This is needed to make sure to avoid using the wrong architecture
                # in the compiler checks CMake performs
                -DCMAKE_OSX_ARCHITECTURES="${architecture}"
            )

            lldb_cmake_options+=(
                -DCMAKE_OSX_SYSROOT:PATH="${cmake_os_sysroot}"
            )
            ;;
    esac


    # We don't currently support building compiler-rt for cross-compile targets.
    # It's not clear that's useful anyway.
    if [[ $(is_cross_tools_host "${host}") ]] ; then
      llvm_cmake_options+=(
          -DLLVM_TOOL_COMPILER_RT_BUILD:BOOL=FALSE
          -DLLVM_BUILD_EXTERNAL_COMPILER_RT:BOOL=FALSE
      )
    else
      llvm_cmake_options+=(
          -DLLVM_TOOL_COMPILER_RT_BUILD:BOOL="$(false_true ${SKIP_BUILD_COMPILER_RT})"
          -DLLVM_BUILD_EXTERNAL_COMPILER_RT:BOOL="$(false_true ${SKIP_BUILD_COMPILER_RT})"
      )
    fi

    # If we are asked to not generate test targets for LLVM and or Swift,
    # disable as many LLVM tools as we can. This improves compile time when
    # compiling with LTO.
    #
    # *NOTE* Currently we do not support testing LLVM via build-script. But in a
    # future commit we will.
    #for arg in "$(compute_cmake_llvm_tool_disable_flags)"; do
    #    llvm_cmake_options+=( ${arg} )
    #done

    if [[ "${llvm_target_arch}" ]] ; then
        llvm_cmake_options+=(
            -DLLVM_TARGET_ARCH="${llvm_target_arch}"
        )
    fi

    # For cross-compilable hosts, we need to know the triple
    # and it must be the same for both LLVM and Swift

    if [[ "${SWIFT_HOST_TRIPLE}" ]] ; then
        llvm_cmake_options+=(
            -DLLVM_HOST_TRIPLE:STRING="${SWIFT_HOST_TRIPLE}"
        )
        swift_cmake_options+=(
            -DSWIFT_HOST_TRIPLE:STRING="${SWIFT_HOST_TRIPLE}"
        )
        lldb_cmake_options+=(
            -DLLVM_HOST_TRIPLE:STRING="${SWIFT_HOST_TRIPLE}"
        )
    fi
    swift_cmake_options+=(
        -DSWIFT_HOST_VARIANT="${SWIFT_HOST_VARIANT}"
        -DSWIFT_HOST_VARIANT_SDK="${SWIFT_HOST_VARIANT_SDK}"
        -DSWIFT_HOST_VARIANT_ARCH="${SWIFT_HOST_VARIANT_ARCH}"
    )

    llvm_cmake_options+=(
        -DLLVM_LIT_ARGS="${LLVM_LIT_ARGS} -j ${BUILD_JOBS}"
    )
    swift_cmake_options+=(
        -DLLVM_LIT_ARGS="${LLVM_LIT_ARGS} -j ${BUILD_JOBS}"
    )

    if [[ "${CLANG_PROFILE_INSTR_USE}" ]]; then
        llvm_cmake_options+=(
            -DLLVM_PROFDATA_FILE="${CLANG_PROFILE_INSTR_USE}"
        )
    fi

    if [[ "${SWIFT_PROFILE_INSTR_USE}" ]]; then
        swift_cmake_options+=(
            -DSWIFT_PROFDATA_FILE="${SWIFT_PROFILE_INSTR_USE}"
        )
    fi
    swift_cmake_options+=(
        -DCOVERAGE_DB="${COVERAGE_DB}"
    )
}

function configure_default_options() {
    # Build a table of all of the known setting variables names.
    #
    # This is an optimization to do the argument to variable conversion (which is
    # slow) in a single pass.
    local all_settings=()
    for ((i = 0; i < ${#KNOWN_SETTINGS[@]}; i += 3)); do
        all_settings+=("${KNOWN_SETTINGS[i]}")
    done
    local known_setting_varnames=($(to_varname "${all_settings[*]}"))

    # Build up an "associative array" mapping setting names to variable names
    # (we use this for error checking to identify "known options", and as a fast
    # way to map the setting name to a variable name). See the code for scanning
    # command line arguments.
    #
    # This loop also sets (or unsets) each corresponding variable to its default
    # value.
    #
    # NOTE: If the Mac's bash were not stuck in the past, we could "declare -A"
    # an associative array, but instead we have to hack it by defining variables.
    for ((i = 0; i < ${#KNOWN_SETTINGS[@]}; i += 3)); do
        local setting="${KNOWN_SETTINGS[i]}"
        local default_value="${KNOWN_SETTINGS[$((i+1))]}"

        # Find the variable name in our lookup table.
        local varname="${known_setting_varnames[$((i/3))]}"

        # Establish the associative array mapping.
        eval "${setting//-/_}_VARNAME=${varname}"

        if [[ "${default_value}" ]] ; then
            # For an explanation of the backslash see http://stackoverflow.com/a/9715377
            eval ${varname}=$\default_value
        else
            unset ${varname}
        fi
    done
}
configure_default_options

COMMAND_NAME="$(basename "$0")"

# Print instructions for using this script to stdout
usage() {
    echo "Usage: ${COMMAND_NAME} [--help|-h] [ --SETTING=VALUE | --SETTING VALUE | --SETTING ]*"
    echo
    echo "  Available settings. Each setting corresponds to a variable,"
    echo "  obtained by upcasing its name, in this script.  A variable"
    echo "  with no default listed here will be unset in the script if"
    echo "  not explicitly specified.  A setting passed in the 3rd form"
    echo "  will set its corresponding variable to \"1\"."
    echo

    setting_list="
 | |Setting| Default|Description
 | |-------| -------|-----------
"

    for ((i = 0; i < ${#KNOWN_SETTINGS[@]}; i += 3)); do
        setting_list+="\
 | |--${KNOWN_SETTINGS[i]}| ${KNOWN_SETTINGS[$((i+1))]}|${KNOWN_SETTINGS[$((i+2))]}
"
    done
    echo "${setting_list}" | column -x -s'|' -t
    echo
    echo "Note: when using the form --SETTING VALUE, VALUE must not begin "
    echo "      with a hyphen."
    echo "Note: the \"--release\" option creates a pre-packaged combination"
    echo "      of settings used by the buildbot."
    echo
    echo "Cross-compiling Swift host tools"
    echo "  When building cross-compiled tools, it first builds for the native"
    echo "  build host machine. Then it proceeds to build the specified cross-compile"
    echo "  targets. It currently builds the requested variants of stdlib each"
    echo "  time around, so once for the native build, then again each time for"
    echo "  the cross-compile tool targets."
    echo
    echo "  When installing cross-compiled tools, it first installs each target"
    echo "  arch into a separate subdirectory under install-destdir, since you"
    echo "  can cross-compile for multiple targets at the same time. It then runs"
    echo "  recursive-lipo to produce fat binaries by merging the cross-compiled"
    echo "  targets, installing the merged result into the expected location of"
    echo "  install-destdir. After that, any remaining steps to extract dsyms and"
    echo "  create an installable package operates on install-destdir as normal."
}

# Scan all command-line arguments
while [[ "$1" ]] ; do
    case "$1" in
        -h | --help )
            usage
            exit
            ;;

        --* )
            dashless="${1:2}"

            # drop suffix beginning with the first "="
            setting="${dashless%%=*}"

            # compute the variable to set, using the cached map set up by
            # configure_default_options().
            varname_var="${setting//-/_}_VARNAME"
            varname=${!varname_var}

            # check to see if this is a known option
            if [[ "${varname}" = "" ]] ; then
                echo "error: unknown setting: ${setting}" 1>&2
                usage 1>&2
                exit 1
            fi

            # find the intended value
            if [[ "${dashless}" == *=* ]] ; then              # if there's an '=', the value
                value="${dashless#*=}"                        #   is everything after the first '='
            elif [[ "$2" ]] && [[ "${2:0:1}" != "-" ]] ; then # else if the next parameter exists
                value="$2"                                    #   but isn't an option, use that
                shift
            else                                              # otherwise, the value is 1
                value=1
            fi

            # For explanation of backslash see http://stackoverflow.com/a/9715377
            eval ${varname}=$\value
            ;;

        *)
            echo "Error: Invalid argument: $1" 1>&2
            usage 1>&2
            exit 1
    esac
    shift
done

# TODO: Rename this argument
LOCAL_HOST=$HOST_TARGET

if [[ "${CHECK_ARGS_ONLY}" ]]; then
    exit 0
fi

# FIXME: We currently do not support building compiler-rt with the
# Xcode generator.
if [[ "${CMAKE_GENERATOR}" == "Xcode" ]]; then
    SKIP_BUILD_COMPILER_RT=1
fi

if [[ "${SKIP_RECONFIGURE}" ]]; then
    RECONFIGURE=""
fi

# WORKSPACE, BUILD_DIR, and INSTALLABLE_PACKAGE must be absolute paths
if ! [[ ${WORKSPACE} =~ ^/ ]] ; then
  echo "workspace must be an absolute path (was '${WORKSPACE}')"
  exit 1
fi
if ! [[ ${BUILD_DIR} =~ ^/ ]] ; then
  echo "build-dir must be an absolute path (was '${BUILD_DIR}')"
  exit 1
fi
if ! [[ ${INSTALLABLE_PACKAGE:-/} =~ ^/ ]] ; then
  echo "installable-package must be an absolute path (was '${INSTALLABLE_PACKAGE}')"
  exit 1
fi

# WORKSPACE must exist
if [ ! -e "${WORKSPACE}" ] ; then
    echo "Workspace does not exist (tried ${WORKSPACE})"
    exit 1
fi

# FIXME: HOST_CC and CMAKE are set, and their presence is validated by,
#        utils/build-script. These checks are redundant, but must remain until
#        build-script-impl is merged completely with utils/build-script.
#        For additional information, see: https://bugs.swift.org/browse/SR-237
if [ -z "${HOST_CC}" ] ; then
    echo "Can't find clang.  Please install clang-3.5 or a later version."
    exit 1
fi
if [ -z "${CMAKE}" ] ; then
    echo "Environment variable CMAKE must be specified."
    exit 1
fi

function xcrun_find_tool() {
  xcrun --sdk macosx --toolchain "${DARWIN_XCRUN_TOOLCHAIN}" --find "$@"
}

function find_just_built_local_host_llvm_tool() {
  find $(build_directory "${LOCAL_HOST}" llvm) -name "$1" -type f -print
}

function not() {
    if [[ ! "$1" ]] ; then
        echo 1
    fi
}

function join {
    local IFS="$1"; shift; echo "$*";
}

function false_true() {
    if [[ $(true_false "$1") = "TRUE" ]]; then
        echo "FALSE"
    else
        echo "TRUE"
    fi
}

# Sanitize the list of cross-compilation targets.
#
# In the Build/Host/Target paradigm:
# - "LOCAL_HOST" is Build (local machine running this script)
# - "CROSS_COMPILE_HOSTS" are the Hosts (implicitly includes LOCAL_HOST)
# - "STDLIB_DEPLOYMENT_TARGETS" are the Targets (for configuration)
# - "BUILD_STDLIB_DEPLOYMENT_TARGETS" are the Targets to build in this invocation

CROSS_COMPILE_HOSTS=($CROSS_COMPILE_HOSTS)
for t in "${CROSS_COMPILE_HOSTS[@]}"; do
    case ${t} in
        macosx-arm64* | iphone* | appletv* | watch* | linux-armv6 | linux-armv7 | android-* )
            ;;
        *)
            echo "Unknown host to cross-compile for: ${t}"
            exit 1
            ;;
    esac
done

ALL_HOSTS=()
if [[ ! "${SKIP_LOCAL_BUILD}" ]] ; then
    ALL_HOSTS=("${ALL_HOSTS[@]}" "${LOCAL_HOST}")
fi
ALL_HOSTS=("${ALL_HOSTS[@]}" "${CROSS_COMPILE_HOSTS[@]}")

function has_cross_compile_hosts() {
    if [[ ${#CROSS_COMPILE_HOSTS[@]} -ge 1 ]]; then
        echo "1"
    fi
}

# We install in to host-specific directories when building more than one host.
# Other users will expect their products at INSTALL_DESTDIR.
function get_host_install_destdir() {
   local host="$1"

    if [[ $(has_cross_compile_hosts) ]]; then
        # If cross compiling tools, install into a host-specific subdirectory.
        if [[ $(should_include_host_in_lipo ${host}) ]]; then
            # If this is one of the hosts we should lipo, install in to a temporary subdirectory.
            local host_install_destdir="${BUILD_DIR}/intermediate-install/${host}"
        elif [[ "${host}" == "merged-hosts" ]]; then
            # This assumes that all hosts are merged to the lipo.
            local host_install_destdir="${INSTALL_DESTDIR}"
        else
            local host_install_destdir="${INSTALL_DESTDIR}/${host}"
        fi
    else
        local host_install_destdir="${INSTALL_DESTDIR}"
    fi

    echo "${host_install_destdir}/" # Should always end in a '/'; it's a directory.
}

function splitSemicolonDelimitedInstallPrefixes() {
    local IFS=";"; CROSS_COMPILE_INSTALL_PREFIXES=($CROSS_COMPILE_INSTALL_PREFIXES)
}
splitSemicolonDelimitedInstallPrefixes

function get_host_install_prefix() {
    local host="$1"

    if [[ $(is_cross_tools_host ${host}) ]] && [[ ${#CROSS_COMPILE_INSTALL_PREFIXES[@]} -gt 0 ]]; then

        # Find the host's index in CROSS_COMPILE_HOSTS.
        for i in "${!CROSS_COMPILE_HOSTS[@]}"; do
           if [[ "${CROSS_COMPILE_HOSTS[$i]}" == "${host}" ]]; then
               local host_index=i
           fi
        done

        if [[ ${host_index} -lt ${#CROSS_COMPILE_INSTALL_PREFIXES[@]} ]]; then
            local host_install_prefix="${CROSS_COMPILE_INSTALL_PREFIXES[${host_index}]}"
        else
            # If there is no explicit install prefix for this host, use the last one
            # in the list.
            local host_install_prefix="${CROSS_COMPILE_INSTALL_PREFIXES[${#CROSS_COMPILE_INSTALL_PREFIXES[@]}-1]}"
        fi
    else
        local host_install_prefix="${INSTALL_PREFIX}"
    fi

    # Should always begin with a '/'; otherwise CMake will expand it as a relative path from the build folder.
    if [[ "${host_install_prefix:0:1}" != "/" ]]; then
        host_install_prefix="/${host_install_prefix}"
    fi

    echo "${host_install_prefix}/" # Should always end in a '/'; it's a directory.
}

function is_cross_tools_host() {
    local host="$1"
    for t in "${CROSS_COMPILE_HOSTS[@]}" ; do
        if [ "${host}" == "${t}" ] ; then
            echo 1
        fi
    done
}

# When building cross-compilers for these hosts,
# merge all of their contents together with lipo
function should_include_host_in_lipo() {
    local host="$1"
    if [[ $(has_cross_compile_hosts) ]] && [[ -z "${SKIP_MERGE_LIPO_CROSS_COMPILE_TOOLS}" ]]; then
        case ${host} in
            macosx* | iphone* | appletv* | watch* )
                echo 1
                ;;
        esac
    fi
}

function host_has_darwin_symbols() {
    local host="$1"
    case ${host} in
        macosx* | iphone* | appletv* | watch* )
            echo 1
            ;;
    esac
}

function get_stdlib_targets_for_host() {
    # Don't build the stdlib in the Xcode train for host and cross-compilations host.
    if [[ "${STDLIB_DEPLOYMENT_TARGETS[@]}" == "" ]]; then
        return 0
    fi

# FIXME: STDLIB_DEPLOYMENT_TARGETS argument assumed to apply when Host == Build
# Cross-compile Hosts are only built with their native standard libraries.
# To fix this, we would need to pass in a list of stdlib targets _per host_,
# and the SWIFT_SDKS parameters would need to be able to capture both the SDK
# and architecture of each stdlib target -- currently it only captures the SDK.
#
# We turn these targets in to SWIFT_SDKS in `calculate_targets_for_host()`
    if [[ $(is_cross_tools_host $1) ]] ; then
        echo "$1"
    else
        echo "${STDLIB_DEPLOYMENT_TARGETS[@]}"
    fi
}

#
# Calculate source directories for each product.
#
NINJA_SOURCE_DIR="${WORKSPACE}/ninja"
SWIFT_SOURCE_DIR="${WORKSPACE}/swift"
LLVM_SOURCE_DIR="${WORKSPACE}/llvm-project/llvm"
CMARK_SOURCE_DIR="${WORKSPACE}/cmark"
LLDB_SOURCE_DIR="${WORKSPACE}/llvm-project/lldb"
LLBUILD_SOURCE_DIR="${WORKSPACE}/llbuild"
STRESSTEST_PACKAGE_DIR="${WORKSPACE}/swift-stress-tester"
XCTEST_SOURCE_DIR="${WORKSPACE}/swift-corelibs-xctest"
FOUNDATION_SOURCE_DIR="${WORKSPACE}/swift-corelibs-foundation"
FOUNDATION_STATIC_SOURCE_DIR="${WORKSPACE}/swift-corelibs-foundation"
LIBDISPATCH_SOURCE_DIR="${WORKSPACE}/swift-corelibs-libdispatch"
LIBDISPATCH_STATIC_SOURCE_DIR="${WORKSPACE}/swift-corelibs-libdispatch"
LIBICU_SOURCE_DIR="${WORKSPACE}/icu"
LIBCXX_SOURCE_DIR="${WORKSPACE}/llvm-project/libcxx"

# We cannot currently apply the normal rules of skipping here for LLVM. Even if
# we are skipping building LLVM, we still need to at least build a few tools
# like tblgen that Swift relies on for building and testing. See the LLVM
# configure rules.
PRODUCTS=(llvm)
[[ "${SKIP_BUILD_CMARK}" ]] || PRODUCTS+=(cmark)
[[ "${SKIP_BUILD_LIBCXX}" ]] || PRODUCTS+=(libcxx)
[[ "${SKIP_BUILD_LIBICU}" ]] || PRODUCTS+=(libicu)
[[ "${SKIP_BUILD_SWIFT}" ]] || PRODUCTS+=(swift)
[[ "${SKIP_BUILD_LLDB}" ]] || PRODUCTS+=(lldb)
[[ "${SKIP_BUILD_LIBDISPATCH}" ]] || PRODUCTS+=(libdispatch)
[[ "${SKIP_BUILD_STATIC_LIBDISPATCH}" ]] || PRODUCTS+=(libdispatch_static)
# llbuild and XCTest depend on Foundation, so Foundation must
# be added to the list of build products first.
[[ "${SKIP_BUILD_FOUNDATION}" ]] || PRODUCTS+=(foundation)
[[ "${SKIP_BUILD_STATIC_FOUNDATION}" ]] || PRODUCTS+=(foundation_static)
[[ "${SKIP_BUILD_LLBUILD}" ]] || PRODUCTS+=(llbuild)
[[ "${SKIP_BUILD_XCTEST}" ]] || PRODUCTS+=(xctest)

# get_host_specific_variable(host, name)
#
# Get the value of a host-specific variable expected to have been passed by the
# `build-script`.
#
# This is a total hack, and is part of the SR-237 migration.
function get_host_specific_variable() {
    local host="$1"
    local name="$2"
    local envvar_name="HOST_VARIABLE_${host//-/_}__${name}"
    echo "${!envvar_name}"
}

function calculate_targets_for_host() {
    # Get the values passed by `build-script`.
    SWIFT_STDLIB_TARGETS=($(get_host_specific_variable ${host} SWIFT_STDLIB_TARGETS))
    SWIFT_SDKS=($(get_host_specific_variable ${host} SWIFT_SDKS))
    SWIFT_BENCHMARK_TARGETS=($(get_host_specific_variable ${host} SWIFT_BENCHMARK_TARGETS))
    SWIFT_RUN_BENCHMARK_TARGETS=($(get_host_specific_variable ${host} SWIFT_RUN_BENCHMARK_TARGETS))
    SWIFT_TEST_TARGETS=($(get_host_specific_variable ${host} SWIFT_TEST_TARGETS))
}


COMMON_C_FLAGS=" -Wno-unknown-warning-option -Werror=unguarded-availability-new"

# Convert to an array.
eval COMMON_CMAKE_OPTIONS=(${COMMON_CMAKE_OPTIONS})
eval EXTRA_CMAKE_OPTIONS=(${EXTRA_CMAKE_OPTIONS})
eval BUILD_ARGS=(${BUILD_ARGS})

eval CMAKE_BUILD=("${DISTCC_PUMP}" "${CMAKE}" "--build")


if [[ "${CMAKE_GENERATOR}" == "Xcode" ]]; then
    BUILD_TARGET_FLAG="-target"
fi

function build_directory() {
    host=$1
    product=$2
    echo "${BUILD_DIR}/${product}-${host}"
}

function build_directory_bin() {
    host=$1
    product=$2
    root="$(build_directory ${host} ${product})"
    if [[ "${CMAKE_GENERATOR}" == "Xcode" ]] ; then
        case ${product} in
            cmark)
                echo "${root}/${CMARK_BUILD_TYPE}/bin"
                ;;
            llvm)
                echo "${root}/${LLVM_BUILD_TYPE}/bin"
                ;;
            libcxx)
                # Reuse LLVM's build type.
                echo "${root}/${LLVM_BUILD_TYPE}/bin"
                ;;
            swift)
                echo "${root}/${SWIFT_BUILD_TYPE}/bin"
                ;;
            lldb)
                ;;
            llbuild)
                echo "${root}/${LLBUILD_BUILD_TYPE}/bin"
                ;;
            xctest)
                echo "${root}/${XCTEST_BUILD_TYPE}/bin"
                ;;
            foundation|foundation_static)
                echo "${root}/${FOUNDATION_BUILD_TYPE}/bin"
                ;;
            libdispatch|libdispatch_static)
                echo "${root}/${LIBDISPATCH_BUILD_TYPE}/bin"
                ;;
            libicu)
                ;;
            playgroundsupport)
                echo "${root}/${PLAYGROUNDSUPPORT_BUILD_TYPE}/bin"
                ;;
            *)
                echo "error: unknown product: ${product}"
                exit 1
                ;;
        esac
    else
        echo "${root}/bin"
    fi
}

function is_cmake_release_build_type() {
    if [[ "$1" == "Release" || "$1" == "RelWithDebInfo" ]] ; then
        echo 1
    fi
}

function is_cmake_debuginfo_build_type() {
    if [[ "$1" == "Debug" || "$1" == "RelWithDebInfo" ]] ; then
        echo 1
    fi
}

function common_cross_c_flags() {
    echo -n "${COMMON_C_FLAGS}"

    local host=$1
    local arch=${host##*-}

    case $host in
        macosx-*)
            echo -n " -arch ${arch} "
            ;;
        iphonesimulator-*)
            echo -n " -arch ${arch}  -mios-simulator-version-min=${DARWIN_DEPLOYMENT_VERSION_IOS}"
            ;;
        iphoneos-*)
            echo -n " -arch ${arch}  -miphoneos-version-min=${DARWIN_DEPLOYMENT_VERSION_IOS}"
            ;;
        appletvsimulator-*)
            echo -n " -arch ${arch}  -mtvos-simulator-version-min=${DARWIN_DEPLOYMENT_VERSION_TVOS}"
            ;;
        appletvos-*)
            echo -n " -arch ${arch}  -mtvos-version-min=${DARWIN_DEPLOYMENT_VERSION_TVOS}"
            ;;
        watchsimulator-*)
            echo -n " -arch ${arch}  -mwatchos-simulator-version-min=${DARWIN_DEPLOYMENT_VERSION_WATCHOS}"
            ;;
        watchos-*)
            echo -n " -arch ${arch}  -mwatchos-version-min=${DARWIN_DEPLOYMENT_VERSION_WATCHOS}"
            ;;
    esac

    local build_type=$2
    if [[ $(is_cmake_release_build_type ${build_type}) ]] ; then
      echo -n " -fno-stack-protector"
    fi
}

function llvm_c_flags() {
    echo -n " $(common_cross_c_flags $1 ${LLVM_BUILD_TYPE})"
    if [[ $(is_cmake_debuginfo_build_type "${LLVM_BUILD_TYPE}") ]] ; then
        if [[ $(is_llvm_lto_enabled) == "TRUE" ]] ; then
            echo -n " -gline-tables-only"
        else
            echo -n " -g"
        fi
    fi
}

function cmark_c_flags() {
    echo -n " $(common_cross_c_flags $1 ${CMARK_BUILD_TYPE})"
}

function swift_c_flags() {
    # Don't pass common_cross_c_flags to Swift because CMake code in the Swift
    # project is itself aware of cross-compilation for the host tools and
    # standard library.
    echo -n "${COMMON_C_FLAGS}"
    if [[ $(is_cmake_release_build_type "${SWIFT_BUILD_TYPE}") ]] ; then
        echo -n " -fno-stack-protector"
    fi
}

function cmake_config_opt() {
    product=$1
    if [[ "${CMAKE_GENERATOR}" == "Xcode" ]] ; then
        # CMake automatically adds --target ALL_BUILD if we don't pass this.
        echo "--target ZERO_CHECK "
        case ${product} in
            cmark)
                echo "--config ${CMARK_BUILD_TYPE}"
                ;;
            llvm)
                echo "--config ${LLVM_BUILD_TYPE}"
                ;;
            libcxx)
                # Reuse LLVM's build type.
                echo "--config ${LLVM_BUILD_TYPE}"
                ;;
            swift)
                echo "--config ${SWIFT_BUILD_TYPE}"
                ;;
            lldb)
                ;;
            llbuild)
                echo "--config ${LLBUILD_BUILD_TYPE}"
                ;;
            xctest)
                echo "--config ${XCTEST_BUILD_TYPE}"
                ;;
            foundation|foundation_static)
                echo "--config ${FOUNDATION_BUILD_TYPE}"
                ;;
            libdispatch|libdispatch_static)
                echo "--config ${LIBDISPATCH_BUILD_TYPE}"
                ;;
            libicu)
                ;;
            playgroundsupport)
                echo "--config ${PLAYGROUNDSUPPORT_BUILD_TYPE}"
                ;;
            *)
                echo "error: unknown product: ${product}"
                exit 1
                ;;
        esac
    fi
}

#
# Configure and build each product
#
# Start with native deployment targets because the resulting tools are used during cross-compilation.


for host in "${ALL_HOSTS[@]}"; do
    # Skip this pass when the only action to execute can't match.
    if ! [[ $(should_execute_host_actions_for_phase ${host} build) ]]; then
        continue
    fi

    calculate_targets_for_host $host

    set_build_options_for_host $host

    # Don't echo anything if only executing an individual action.
    if [[ "${ONLY_EXECUTE}" = "all" ]]; then
        echo "Building the standard library for: ${SWIFT_STDLIB_TARGETS[@]}"
        if [[ "${SWIFT_TEST_TARGETS[@]}" ]] && ! [[ "${SKIP_TEST_SWIFT}" ]]; then
            echo "Running Swift tests for: ${SWIFT_TEST_TARGETS[@]}"
        fi
        if ! [[ "${SKIP_TEST_BENCHMARKS}" ]] &&
             [[ "${SWIFT_RUN_BENCHMARK_TARGETS[@]}" ]]; then
            echo "Running Swift benchmarks for: ${SWIFT_RUN_BENCHMARK_TARGETS[@]}"
        fi
    fi

    common_cmake_options_host=("${COMMON_CMAKE_OPTIONS[@]}")

    if [[ $(is_cross_tools_host ${host}) ]] ; then

        if [[ "${CROSS_COMPILE_WITH_HOST_TOOLS}" ]]; then
            # Optionally use the freshly-built host copy of clang to build
            # for foreign hosts.
            common_cmake_options_host+=(
                -DCMAKE_C_COMPILER="$(build_directory ${LOCAL_HOST} llvm)/bin/clang"
                -DCMAKE_CXX_COMPILER="$(build_directory ${LOCAL_HOST} llvm)/bin/clang++"
            )
        fi

        # CMake can't relink when using Ninja, but that's okay -
        # we don't need a build-local rpath because we can't run cross-compiled products
        if [[ "${CMAKE_GENERATOR}" == "Ninja" ]]; then
            common_cmake_options_host+=(
                -DCMAKE_BUILD_WITH_INSTALL_RPATH="1"
            )
        fi
    fi

    llvm_cmake_options=(
        "${llvm_cmake_options[@]}"
        -DCMAKE_INSTALL_PREFIX:PATH="$(get_host_install_prefix ${host})"
        -DINTERNAL_INSTALL_PREFIX="local"
    )

    if [[ "$(uname -s)" == "Linux" ]] ; then
        if [[ $(is_cmake_debuginfo_build_type "${LLVM_BUILD_TYPE}") ]] ; then
            # On Linux build LLVM and subprojects with -gsplit-dwarf which is more
            # space/time efficient than -g on that platform.
            llvm_cmake_options=(
                "${llvm_cmake_options[@]}"
                -DLLVM_USE_SPLIT_DWARF:BOOL=YES
            )
        fi
    fi

    if [[ "${DARWIN_TOOLCHAIN_VERSION}" ]] ; then
        swift_cmake_options=(
            "${swift_cmake_options[@]}"
            -DDARWIN_TOOLCHAIN_VERSION="${DARWIN_TOOLCHAIN_VERSION}"
        )
    fi

    if [[ "${ENABLE_ASAN}" || "$(uname -s)" == "Linux" ]] ; then
        swift_cmake_options=(
            "${swift_cmake_options[@]}"
            -DSWIFT_SOURCEKIT_USE_INPROC_LIBRARY:BOOL=TRUE
        )
    fi

    if [[ "${DARWIN_CRASH_REPORTER_CLIENT}" ]] ; then
        swift_cmake_options=(
            "${swift_cmake_options[@]}"
            -DSWIFT_RUNTIME_CRASH_REPORTER_CLIENT:BOOL=TRUE
        )
    fi

    swift_cmake_options=(
        "${swift_cmake_options[@]}"
        -DSWIFT_DARWIN_XCRUN_TOOLCHAIN:STRING="${DARWIN_XCRUN_TOOLCHAIN}"
    )

    if [[ "${DARWIN_STDLIB_INSTALL_NAME_DIR}" ]] ; then
        swift_cmake_options=(
            "${swift_cmake_options[@]}"
            -DSWIFT_DARWIN_STDLIB_INSTALL_NAME_DIR:STRING="${DARWIN_STDLIB_INSTALL_NAME_DIR}"
        )
    fi

    if [[ "${EXTRA_SWIFT_ARGS}" ]] ; then
        swift_cmake_options=(
            "${swift_cmake_options[@]}"
            -DSWIFT_EXPERIMENTAL_EXTRA_REGEXP_FLAGS="${EXTRA_SWIFT_ARGS}"
        )
    fi

    swift_cmake_options=(
        "${swift_cmake_options[@]}"
        -DSWIFT_AST_VERIFIER:BOOL=$(true_false "${SWIFT_ENABLE_AST_VERIFIER}")
        -DSWIFT_SIL_VERIFY_ALL:BOOL=$(true_false "${SIL_VERIFY_ALL}")
        -DSWIFT_RUNTIME_ENABLE_LEAK_CHECKER:BOOL=$(true_false "${SWIFT_RUNTIME_ENABLE_LEAK_CHECKER}")
    )

    if [[ "${TEST_PATHS}" ]]; then
        build_dir=$(build_directory ${host} "swift")

        test_paths=()
        for path in ${TEST_PATHS}; do
            test_paths+=(
                "${build_dir}/$(echo ${path} | sed -E "s/^(validation-test|test)/\1-${host}/")"
            )
        done

        swift_cmake_options=(
            "${swift_cmake_options[@]}"
            -DSWIFT_LIT_TEST_PATHS="${test_paths}")
    fi

    if [[ "${SKIP_TEST_SOURCEKIT}" ]] ; then
        swift_cmake_options=(
            "${swift_cmake_options[@]}"
            -DSWIFT_ENABLE_SOURCEKIT_TESTS:BOOL=FALSE
        )
    fi

    if [[ "${NATIVE_CLANG_TOOLS_PATH}" ]] ; then
        CLANG_BIN="${NATIVE_CLANG_TOOLS_PATH}"
    else
        CLANG_BIN="$(build_directory_bin ${LOCAL_HOST} llvm)"
    fi

    if [[ "${NATIVE_SWIFT_TOOLS_PATH}" ]] ; then
        SWIFTC_BIN="${NATIVE_SWIFT_TOOLS_PATH}/swiftc"
    else
        SWIFTC_BIN="$(build_directory_bin ${LOCAL_HOST} swift)/swiftc"
    fi

    for product in "${PRODUCTS[@]}"; do
        [[ $(should_execute_action "${host}-${product/_static}-build") ]] || continue

        source_dir_var="$(toupper ${product})_SOURCE_DIR"
        source_dir=${!source_dir_var}
        build_dir=$(build_directory ${host} ${product})
        build_targets=(all)

        cmake_options=("${common_cmake_options_host[@]}")

        llvm_build_dir=$(build_directory ${host} llvm)
        module_cache="${build_dir}/module-cache"

        # Add any specific cmake options specified by build-script
        product_cmake_options_name=$(to_varname "${product/_static}")_CMAKE_OPTIONS
        product_cmake_options=(${!product_cmake_options_name}) # convert to array
        cmake_options+=("${product_cmake_options[@]}")

        case ${product} in
            cmark)
                cmake_options=(
                  "${cmake_options[@]}"
                  -DCMAKE_BUILD_TYPE:STRING="${CMARK_BUILD_TYPE}"
                  "${cmark_cmake_options[@]}"
                )
                build_targets=(all)
                ;;

            llvm)
                if [ "${BUILD_LLVM}" == "0" ] ; then
                    build_targets=(clean)
                fi
                if [[ "${SKIP_BUILD}" || "${SKIP_BUILD_LLVM}" ]] ; then
                    # We can't skip the build completely because the standalone
                    # build of Swift depend on these for building and testing.
                    build_targets=(llvm-tblgen clang-resource-headers intrinsics_gen clang-tablegen-targets)
                    # If we are not performing a toolchain only build, then we
                    # also want to include FileCheck and not for testing
                    # purposes.
                    if [[ ! "${BUILD_TOOLCHAIN_ONLY}" ]] ; then
                      build_targets=(
                          "${build_targets[@]}"
                          FileCheck
                          not
                      )
                    fi
                fi

                if [ "${HOST_LIBTOOL}" ] ; then
                    cmake_options=(
                        "${cmake_options[@]}"
                        -DCMAKE_LIBTOOL:PATH="${HOST_LIBTOOL}"
                    )
                fi

                # Note: we set the variable:
                #
                # LLVM_TOOL_SWIFT_BUILD
                #
                # below because this script builds swift separately, and people
                # often have reasons to symlink the swift directory into
                # llvm/tools, e.g. to build LLDB.
                cmake_options=(
                    "${cmake_options[@]}"
                    -DCMAKE_C_FLAGS="$(llvm_c_flags ${host})"
                    -DCMAKE_CXX_FLAGS="$(llvm_c_flags ${host})"
                    -DCMAKE_C_FLAGS_RELWITHDEBINFO="-O2 -DNDEBUG"
                    -DCMAKE_CXX_FLAGS_RELWITHDEBINFO="-O2 -DNDEBUG"
                    -DCMAKE_BUILD_TYPE:STRING="${LLVM_BUILD_TYPE}"
                    -DLLVM_TOOL_SWIFT_BUILD:BOOL=NO
                    -DLLVM_TOOL_LLD_BUILD:BOOL=TRUE
                    -DLLVM_INCLUDE_DOCS:BOOL=TRUE
                    -DLLVM_ENABLE_LTO:STRING="${LLVM_ENABLE_LTO}"
                    -DCOMPILER_RT_INTERCEPT_LIBDISPATCH=ON
                    "${llvm_cmake_options[@]}"
                )

                llvm_enable_projects=("clang")

                if [[ ! "${SKIP_BUILD_COMPILER_RT}" && ! $(is_cross_tools_host ${host}) ]]; then
                    llvm_enable_projects+=("compiler-rt")
                fi

                if [[ ! "${SKIP_BUILD_CLANG_TOOLS_EXTRA}" ]]; then
                    llvm_enable_projects+=("clang-tools-extra")
                fi

                # On non-Darwin platforms, build lld so we can always have a
                # linker that is compatible with the swift we are using to
                # compile the stdlib.
                #
                # This makes it easier to build target stdlibs on systems that
                # have old toolchains without more modern linker features.
                if [[ "$(uname -s)" != "Darwin" ]] ; then
                    if [[ ! "${SKIP_BUILD_LLD}" ]]; then
                        llvm_enable_projects+=("lld")
                    fi
                fi

                cmake_options+=(
                    -DLLVM_ENABLE_PROJECTS="$(join ";" ${llvm_enable_projects[@]})"
                )

                if [[ "${BUILD_TOOLCHAIN_ONLY}" ]]; then
                    cmake_options+=(
                    -DLLVM_BUILD_TOOLS=NO
                    -DLLVM_INSTALL_TOOLCHAIN_ONLY=YES
                    -DLLVM_INCLUDE_TESTS=NO
                    -DCLANG_INCLUDE_TESTS=NO
                    -DLLVM_INCLUDE_UTILS=NO
                    -DLLVM_TOOL_LLI_BUILD=NO
                    -DLLVM_TOOL_LLVM_AR_BUILD=NO
                    -DCLANG_TOOL_CLANG_CHECK_BUILD=NO
                    -DCLANG_TOOL_ARCMT_TEST_BUILD=NO
                    -DCLANG_TOOL_C_ARCMT_TEST_BUILD=NO
                    -DCLANG_TOOL_C_INDEX_TEST_BUILD=NO
                    -DCLANG_TOOL_DRIVER_BUILD=$(false_true "${BUILD_RUNTIME_WITH_HOST_COMPILER}")
                    -DCLANG_TOOL_DIAGTOOL_BUILD=NO
                    -DCLANG_TOOL_SCAN_BUILD_BUILD=NO
                    -DCLANG_TOOL_SCAN_VIEW_BUILD=NO
                    -DCLANG_TOOL_CLANG_FORMAT_BUILD=NO
                    )
                fi

                if [[ $(true_false "${LLVM_INCLUDE_TESTS}") == "FALSE" ]]; then
                    cmake_options+=(
                        -DLLVM_INCLUDE_TESTS=NO
                        -DCLANG_INCLUDE_TESTS=NO
                    )
                fi

                if [[ $(is_cross_tools_host ${host}) ]] ; then
                    cmake_options=(
                        "${cmake_options[@]}"
                        -DLLVM_TABLEGEN=$(build_directory "${LOCAL_HOST}" llvm)/bin/llvm-tblgen
                        -DCLANG_TABLEGEN=$(build_directory "${LOCAL_HOST}" llvm)/bin/clang-tblgen
                        -DLLVM_NATIVE_BUILD=$(build_directory "${LOCAL_HOST}" llvm)
                    )
                fi

                ;;

            libcxx)
                build_targets=()
                cmake_options=(
                    "${cmake_options[@]}"
                    "${llvm_cmake_options[@]}"
                )
                ;;

            swift)

                if [[ "${ANDROID_API_LEVEL}" ]]; then
                    cmake_options=(
                        "${cmake_options[@]}"
                        -DSWIFT_ANDROID_API_LEVEL:STRING="${ANDROID_API_LEVEL}"
                    )
                fi

                if [[ ! "${SKIP_BUILD_ANDROID}" ]]; then
                    cmake_options=(
                        "${cmake_options[@]}"
                        -DSWIFT_ANDROID_NDK_PATH:STRING="${ANDROID_NDK}"
                        -DSWIFT_ANDROID_NDK_GCC_VERSION:STRING="${ANDROID_NDK_GCC_VERSION}"
                        -DSWIFT_ANDROID_${ANDROID_ARCH}_ICU_UC:STRING="${ANDROID_ICU_UC}"
                        -DSWIFT_ANDROID_${ANDROID_ARCH}_ICU_UC_INCLUDE:STRING="${ANDROID_ICU_UC_INCLUDE}"
                        -DSWIFT_ANDROID_${ANDROID_ARCH}_ICU_I18N:STRING="${ANDROID_ICU_I18N}"
                        -DSWIFT_ANDROID_${ANDROID_ARCH}_ICU_I18N_INCLUDE:STRING="${ANDROID_ICU_I18N_INCLUDE}"
                        -DSWIFT_ANDROID_${ANDROID_ARCH}_ICU_DATA:STRING="${ANDROID_ICU_DATA}"
                        -DSWIFT_ANDROID_DEPLOY_DEVICE_PATH:STRING="${ANDROID_DEPLOY_DEVICE_PATH}"
                        -DSWIFT_SDK_ANDROID_ARCHITECTURES:STRING="${ANDROID_ARCH}"
                    )
                fi

                if [[ "${DARWIN_OVERLAY_TARGET}" != "" ]]; then
                    # Split LOCAL_HOST into a pair ``arch-sdk``
                    # Example LOCAL_HOST: macosx-x86_64
                    [[ ${LOCAL_HOST} =~ (.*)-(.*) ]]
                    overlay_target_closure_cmd="${SWIFT_SOURCE_DIR}/utils/find-overlay-deps-closure.sh ${DARWIN_OVERLAY_TARGET} ${BASH_REMATCH[1]} ${BASH_REMATCH[2]}"
                    overlay_target_closure=$($overlay_target_closure_cmd)
                    swift_cmake_options=(
                        "${swift_cmake_options[@]}"
                        "-DSWIFT_OVERLAY_TARGETS:STRING=${overlay_target_closure}"
                    )
                fi

                native_llvm_tools_path=""
                native_clang_tools_path=""
                native_swift_tools_path=""
                if [[ $(is_cross_tools_host ${host}) ]] ; then

                    # Don't build benchmarks and tests when building cross compiler.
                    build_perf_testsuite_this_time=false
                    build_tests_this_time=false

                    native_llvm_tools_path="$(build_directory "${LOCAL_HOST}" llvm)/bin"
                    native_clang_tools_path="$(build_directory "${LOCAL_HOST}" llvm)/bin"
                    native_swift_tools_path="$(build_directory "${LOCAL_HOST}" swift)/bin"
                else
                    # FIXME: Why is the next line not using false_true?
                    build_perf_testsuite_this_time=$(true_false "$(not ${SKIP_BUILD_BENCHMARKS})")
                    build_tests_this_time=${SWIFT_INCLUDE_TESTS}
                fi

                if [[ $(is_cross_tools_host ${host}) ]] ; then
                    cmake_options=(
                        "${cmake_options[@]}"
                        -DLLVM_TABLEGEN=$(build_directory "${LOCAL_HOST}" llvm)/bin/llvm-tblgen
                    )
                fi

                # Command-line parameters override any autodetection that we
                # might have done.
                if [[ "${NATIVE_LLVM_TOOLS_PATH}" ]] ; then
                    native_llvm_tools_path="${NATIVE_LLVM_TOOLS_PATH}"
                fi
                if [[ "${NATIVE_CLANG_TOOLS_PATH}" ]] ; then
                    native_clang_tools_path="${NATIVE_CLANG_TOOLS_PATH}"
                fi
                if [[ "${NATIVE_SWIFT_TOOLS_PATH}" ]] ; then
                    native_swift_tools_path="${NATIVE_SWIFT_TOOLS_PATH}"
                fi

                if [ "${BUILD_LLVM}" == "0" ] ; then
                    cmake_options=(
                        "${cmake_options[@]}"
                        -DLLVM_TOOLS_BINARY_DIR:PATH=/tmp/dummy
                    )
                fi

                if [ "${HOST_LIPO}" ] ; then
                    cmake_options=(
                        "${cmake_options[@]}"
                        -DSWIFT_LIPO:PATH="${HOST_LIPO}"
                    )
                fi

                if [ "${SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS}" == "" ] ; then
                    SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS="${SWIFT_STDLIB_ENABLE_ASSERTIONS}"
                fi


                cmake_options=(
                    "${cmake_options[@]}"
                    -DCMAKE_C_FLAGS="$(swift_c_flags ${host})"
                    -DCMAKE_CXX_FLAGS="$(swift_c_flags ${host})"
                    -DCMAKE_C_FLAGS_RELWITHDEBINFO="-O2 -DNDEBUG"
                    -DCMAKE_CXX_FLAGS_RELWITHDEBINFO="-O2 -DNDEBUG"
                    -DCMAKE_BUILD_TYPE:STRING="${SWIFT_BUILD_TYPE}"
                    -DLLVM_ENABLE_ASSERTIONS:BOOL=$(true_false "${SWIFT_ENABLE_ASSERTIONS}")
                    -DSWIFT_ANALYZE_CODE_COVERAGE:STRING=$(toupper "${SWIFT_ANALYZE_CODE_COVERAGE}")
                    -DSWIFT_STDLIB_BUILD_TYPE:STRING="${SWIFT_STDLIB_BUILD_TYPE}"
                    -DSWIFT_STDLIB_ASSERTIONS:BOOL=$(true_false "${SWIFT_STDLIB_ENABLE_ASSERTIONS}")
                    -DSWIFT_STDLIB_USE_NONATOMIC_RC:BOOL=$(true_false "${SWIFT_STDLIB_USE_NONATOMIC_RC}")
                    -DSWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS:BOOL=$(true_false "${SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS}")
                    -DSWIFT_NATIVE_LLVM_TOOLS_PATH:STRING="${native_llvm_tools_path}"
                    -DSWIFT_NATIVE_CLANG_TOOLS_PATH:STRING="${native_clang_tools_path}"
                    -DSWIFT_NATIVE_SWIFT_TOOLS_PATH:STRING="${native_swift_tools_path}"
                    -DSWIFT_INCLUDE_TOOLS:BOOL=$(true_false "${BUILD_SWIFT_TOOLS}")
                    -DSWIFT_BUILD_REMOTE_MIRROR:BOOL=$(true_false "${BUILD_SWIFT_REMOTE_MIRROR}")
                    -DSWIFT_STDLIB_SIL_DEBUGGING:BOOL=$(true_false "${BUILD_SIL_DEBUGGING_STDLIB}")
                    -DSWIFT_CHECK_INCREMENTAL_COMPILATION:BOOL=$(true_false "${CHECK_INCREMENTAL_COMPILATION}")
                    -DSWIFT_REPORT_STATISTICS:BOOL=$(true_false "${REPORT_STATISTICS}")
                    -DSWIFT_BUILD_DYNAMIC_STDLIB:BOOL=$(true_false "${BUILD_SWIFT_DYNAMIC_STDLIB}")
                    -DSWIFT_BUILD_STATIC_STDLIB:BOOL=$(true_false "${BUILD_SWIFT_STATIC_STDLIB}")
                    -DSWIFT_BUILD_DYNAMIC_SDK_OVERLAY:BOOL=$(true_false "${BUILD_SWIFT_DYNAMIC_SDK_OVERLAY}")
                    -DSWIFT_BUILD_STATIC_SDK_OVERLAY:BOOL=$(true_false "${BUILD_SWIFT_STATIC_SDK_OVERLAY}")
                    -DSWIFT_BUILD_PERF_TESTSUITE:BOOL=$(true_false "${build_perf_testsuite_this_time}")
                    -DSWIFT_BUILD_EXAMPLES:BOOL=$(true_false "${BUILD_SWIFT_EXAMPLES}")
                    -DSWIFT_INCLUDE_TESTS:BOOL=$(true_false "${build_tests_this_time}")
                    -DSWIFT_EMBED_BITCODE_SECTION:BOOL=$(true_false "${EMBED_BITCODE_SECTION}")
                    -DSWIFT_TOOLS_ENABLE_LTO:STRING="${SWIFT_TOOLS_ENABLE_LTO}"
                    -DSWIFT_BUILD_RUNTIME_WITH_HOST_COMPILER:BOOL=$(true_false "${BUILD_RUNTIME_WITH_HOST_COMPILER}")
                    -DLIBDISPATCH_CMAKE_BUILD_TYPE:STRING="${LIBDISPATCH_BUILD_TYPE}"
                    "${swift_cmake_options[@]}"
                )

                cmake_options=(
                    "${cmake_options[@]}"
                    -DCMAKE_INSTALL_PREFIX:PATH="$(get_host_install_prefix ${host})"
                    -DClang_DIR:PATH=${llvm_build_dir}/lib/cmake/clang
                    -DLLVM_DIR:PATH=${llvm_build_dir}/lib/cmake/llvm
                    -DSWIFT_PATH_TO_CMARK_SOURCE:PATH="${CMARK_SOURCE_DIR}"
                    -DSWIFT_PATH_TO_CMARK_BUILD:PATH="$(build_directory ${host} cmark)"
                    -DSWIFT_PATH_TO_LIBDISPATCH_SOURCE:PATH="${LIBDISPATCH_SOURCE_DIR}"
                    -DSWIFT_PATH_TO_LIBDISPATCH_BUILD:PATH="$(build_directory ${host} libdispatch)"
                )

                if [[ ! "${SKIP_BUILD_LIBICU}" ]] ; then
                    LIBICU_BUILD_DIR="$(build_directory ${host} libicu)"
                    ICU_TMPINSTALL=${LIBICU_BUILD_DIR}/tmp_install
                    cmake_options=(
                        "${cmake_options[@]}"
                        -DSWIFT_PATH_TO_LIBICU_SOURCE:PATH="${LIBICU_SOURCE_DIR}"
                        -DSWIFT_PATH_TO_LIBICU_BUILD:PATH="${LIBICU_BUILD_DIR}"
                        -DSWIFT_${SWIFT_HOST_VARIANT_SDK}_${SWIFT_HOST_VARIANT_ARCH}_ICU_UC_INCLUDE:STRING="${ICU_TMPINSTALL}/include"
                        -DSWIFT_${SWIFT_HOST_VARIANT_SDK}_${SWIFT_HOST_VARIANT_ARCH}_ICU_I18N_INCLUDE:STRING="${ICU_TMPINSTALL}/include"
                        -DSWIFT_${SWIFT_HOST_VARIANT_SDK}_${SWIFT_HOST_VARIANT_ARCH}_ICU_STATICLIB:BOOL=TRUE
                    )
                fi

                if [[ "${SWIFT_SDKS}" ]] ; then
                    cmake_options=(
                        "${cmake_options[@]}"
                        -DSWIFT_SDKS:STRING="$(join ";" ${SWIFT_SDKS[@]})"
                    )
                fi

                if [[ "${SWIFT_PRIMARY_VARIANT_SDK}" ]] ; then
                    cmake_options=(
                        "${cmake_options[@]}"
                        -DSWIFT_PRIMARY_VARIANT_SDK:STRING="${SWIFT_PRIMARY_VARIANT_SDK}"
                    )
                fi

                if [[ "${SWIFT_PRIMARY_VARIANT_ARCH}" ]] ; then
                    cmake_options=(
                        "${cmake_options[@]}"
                        -DSWIFT_PRIMARY_VARIANT_ARCH:STRING="${SWIFT_PRIMARY_VARIANT_ARCH}"
                    )
                fi

                if [ "${SWIFT_INSTALL_COMPONENTS}" ] ; then
                    cmake_options=(
                        "${cmake_options[@]}"
                        -DSWIFT_INSTALL_COMPONENTS:STRING="${SWIFT_INSTALL_COMPONENTS}"
                    )
                fi

                if [[ ! "${SKIP_BUILD_LLDB}" ]] ; then
                    lldb_build_dir=$(build_directory ${host} lldb)
                    cmake_options=(
                        "${cmake_options[@]}"
                        -DLLDB_ENABLE:BOOL=TRUE
                        -DLLDB_BUILD_DIR:STRING="${lldb_build_dir}"
                    )
                fi

                build_targets=(all "${SWIFT_STDLIB_TARGETS[@]}")
                if [[ $(true_false "${build_perf_testsuite_this_time}") == "TRUE" ]]; then
                    native_swift_tools_path="$(build_directory_bin ${LOCAL_HOST} swift)"
                    cmake_options=(
                        "${cmake_options[@]}"
                        -DSWIFT_EXEC:STRING="${native_swift_tools_path}/swiftc"
                    )
                    build_targets=("${build_targets[@]}"
                                   "${SWIFT_BENCHMARK_TARGETS[@]}")
                fi
                ;;
            lldb)
                if [ ! -d "${LLDB_SOURCE_DIR}" ]; then
                    echo "error: lldb not found in ${LLDB_SOURCE_DIR}"
                    exit 1
                fi
                if [[ "${CMAKE_GENERATOR}" != "Ninja" ]] ; then
                    echo "error: lldb can only build with ninja"
                    exit 1
                fi
                cmark_build_dir=$(build_directory ${host} cmark)
                lldb_build_dir=$(build_directory ${host} lldb)
                swift_build_dir=$(build_directory ${host} swift)

                # Add any lldb extra cmake arguments here.

                cmake_options=(
                    "${cmake_options[@]}"
                    "${lldb_cmake_options[@]}"
                    )

                # Figure out if we think this is a buildbot build.
                # This will influence the lldb version line.
                if [ ! -z "${JENKINS_HOME}" -a ! -z "${JOB_NAME}" -a ! -z "${BUILD_NUMBER}" ]; then
                    LLDB_IS_BUILDBOT_BUILD=1
                else
                    LLDB_IS_BUILDBOT_BUILD=0
                fi

                # Get the build date.
                LLDB_BUILD_DATE=$(date +%Y-%m-%d)

                # Pick the right cache.
                if [[ "$(uname -s)" == "Darwin" ]] ; then
                  cmake_cache="Apple-lldb-macOS.cmake"
                else
                  cmake_cache="Apple-lldb-Linux.cmake"
                fi

                # Options to find the just-built libddispatch and Foundation.
                if [[ "$(uname -s)" == "Darwin" || "${SKIP_BUILD_FOUNDATION}" ]] ; then
                    DOTEST_EXTRA=""
                else
                    # This assumes that there are no spaces in any on these paths.
                    LIBDISPATCH_BUILD_DIR="$(build_directory ${host} libdispatch)"
                    FOUNDATION_BUILD_DIR=$(build_directory ${host} foundation)
                    DOTEST_EXTRA="-I${FOUNDATION_BUILD_DIR}"
                    DOTEST_EXTRA="${DOTEST_EXTRA} -Xcc -F${FOUNDATION_BUILD_DIR}"
                    DOTEST_EXTRA="${DOTEST_EXTRA} -I${FOUNDATION_BUILD_DIR}/swift"
                    DOTEST_EXTRA="${DOTEST_EXTRA} -I${LIBDISPATCH_SOURCE_DIR}"
                    DOTEST_EXTRA="${DOTEST_EXTRA} -L${LIBDISPATCH_BUILD_DIR}"
                    DOTEST_EXTRA="${DOTEST_EXTRA} -L${LIBDISPATCH_BUILD_DIR}/src"
                    DOTEST_EXTRA="${DOTEST_EXTRA} -L${FOUNDATION_BUILD_DIR}"
                    DOTEST_EXTRA="${DOTEST_EXTRA} -L${FOUNDATION_BUILD_DIR}/Foundation"
                    DOTEST_EXTRA="${DOTEST_EXTRA} -L${FOUNDATION_BUILD_DIR}/Sources/Foundation"
                    DOTEST_EXTRA="${DOTEST_EXTRA} -L${FOUNDATION_BUILD_DIR}/Sources/FoundationNetworking"
                    DOTEST_EXTRA="${DOTEST_EXTRA} -L${FOUNDATION_BUILD_DIR}/Sources/FoundationXML"
                    DOTEST_EXTRA="${DOTEST_EXTRA} -Xlinker -rpath -Xlinker ${LIBDISPATCH_BUILD_DIR}"
                    DOTEST_EXTRA="${DOTEST_EXTRA} -Xlinker -rpath -Xlinker ${LIBDISPATCH_BUILD_DIR}/src"
                    DOTEST_EXTRA="${DOTEST_EXTRA} -Xlinker -rpath -Xlinker ${FOUNDATION_BUILD_DIR}"
                    DOTEST_EXTRA="${DOTEST_EXTRA} -Xlinker -rpath -Xlinker ${FOUNDATION_BUILD_DIR}/Foundation"
                    DOTEST_EXTRA="${DOTEST_EXTRA} -Xlinker -rpath -Xlinker ${FOUNDATION_BUILD_DIR}/Sources/Foundation"
                    DOTEST_EXTRA="${DOTEST_EXTRA} -Xlinker -rpath -Xlinker ${FOUNDATION_BUILD_DIR}/Sources/FoundationNetworking"
                    DOTEST_EXTRA="${DOTEST_EXTRA} -Xlinker -rpath -Xlinker ${FOUNDATION_BUILD_DIR}/Sources/FoundationXML"
                fi

                # Watchpoint testing is currently disabled: see rdar://38566150.
                LLDB_TEST_CATEGORIES="--skip-category=watchpoint"

                # Skip DWO to speed up swift testing.
                if [[ "$(true_false ${LLDB_TEST_SWIFT_ONLY})" == "TRUE" ]]; then
                    LLDB_TEST_CATEGORIES="${LLDB_TEST_CATEGORIES};--skip-category=dwo"
                fi

                # Construct dotest arguments. We use semicolons so CMake interprets this as a list.
                DOTEST_ARGS="--build-dir;${lldb_build_dir}/lldb-test-build.noindex;${LLDB_TEST_CATEGORIES};-t"

                # Only set the extra arguments if they're not empty.
                if [[ -n "${DOTEST_EXTRA}" ]]; then
                    DOTEST_ARGS="${DOTEST_ARGS};-E;${DOTEST_EXTRA}"
                fi

                cmake_options=(
                    "${cmake_options[@]}"
                    -C${LLDB_SOURCE_DIR}/cmake/caches/${cmake_cache}
                    -DCMAKE_C_FLAGS="$(llvm_c_flags ${host})"
                    -DCMAKE_CXX_FLAGS="$(llvm_c_flags ${host})"
                    -DCMAKE_BUILD_TYPE:STRING="${LLDB_BUILD_TYPE}"
                    -DLLDB_SWIFTC:PATH=${SWIFTC_BIN}
                    -DLLDB_SWIFT_LIBS:PATH="$(build_directory ${LOCAL_HOST} swift)/lib/swift"
                    -DCMAKE_INSTALL_PREFIX:PATH="$(get_host_install_prefix ${host})"
                    -DLLDB_FRAMEWORK_INSTALL_DIR="$(get_host_install_prefix ${host})../System/Library/PrivateFrameworks"
                    -DLLVM_DIR:PATH=${llvm_build_dir}/lib/cmake/llvm
                    -DClang_DIR:PATH=${llvm_build_dir}/lib/cmake/clang
                    -DSwift_DIR:PATH=${swift_build_dir}/lib/cmake/swift
                    -DLLDB_ENABLE_CURSES=ON
                    -DLLDB_ENABLE_LIBEDIT=ON
                    -DLLDB_ENABLE_PYTHON=ON
                    -DLLDB_ENABLE_LZMA=OFF
                    -DLLDB_ENABLE_LUA=OFF
                    -DLLDB_PATH_TO_SWIFT_SOURCE:PATH="${SWIFT_SOURCE_DIR}"
                    -DLLDB_IS_BUILDBOT_BUILD:BOOL="${LLDB_IS_BUILDBOT_BUILD}"
                    -DLLDB_BUILD_DATE:STRING="\"${LLDB_BUILD_DATE}\""
                    -DLLDB_INCLUDE_TESTS:BOOL=$(false_true ${BUILD_TOOLCHAIN_ONLY})
                    -DLLDB_TEST_USER_ARGS="${DOTEST_ARGS}"
                )

                if [[ $(is_cross_tools_host ${host}) ]] ; then
                    cmake_options=(
                        "${cmake_options[@]}"
                        -DLLVM_TABLEGEN=$(build_directory "${LOCAL_HOST}" llvm)/bin/llvm-tblgen
                        -DCLANG_TABLEGEN=$(build_directory "${LOCAL_HOST}" llvm)/bin/clang-tblgen
                        -DLLDB_TABLEGEN=$(build_directory "${LOCAL_HOST}" lldb)/bin/lldb-tblgen
                        -DLLDB_TABLEGEN_EXE=$(build_directory "${LOCAL_HOST}" lldb)/bin/lldb-tblgen
                        -DLLVM_NATIVE_BUILD=$(build_directory "${LOCAL_HOST}" llvm)
                    )
                fi

                if [[ "$(uname -s)" == "Darwin" && "$(true_false ${LLDB_USE_SYSTEM_DEBUGSERVER})" == "TRUE" ]]; then
                  cmake_options+=(
                    -DLLDB_USE_SYSTEM_DEBUGSERVER:BOOL="${LLDB_USE_SYSTEM_DEBUGSERVER}"
                  )
                fi

                # Add the extra CMake args at the end so they can override
                # values set earlier.
                if [ ! -z "${LLDB_EXTRA_CMAKE_ARGS}" ]; then
                    cmake_options=(
                        "${cmake_options[@]}"
                        ${LLDB_EXTRA_CMAKE_ARGS}
                        )
                fi
                ;;
            llbuild)
                cmake_options=(
                    "${cmake_options[@]}"

                    -DCMAKE_BUILD_TYPE:STRING="${LLBUILD_BUILD_TYPE}"
                    -DCMAKE_C_COMPILER:PATH="${CLANG_BIN}/clang"
                    -DCMAKE_CXX_COMPILER:PATH="${CLANG_BIN}/clang++"
                    -DCMAKE_INSTALL_PREFIX:PATH="$(get_host_install_prefix ${host})"
                    -DCMAKE_Swift_COMPILER:PATH=${SWIFTC_BIN}

                    -DLLBUILD_ENABLE_ASSERTIONS:BOOL=$(true_false "${LLBUILD_ENABLE_ASSERTIONS}")
                    -DLLBUILD_SUPPORT_BINDINGS:=Swift

                    -DLIT_EXECUTABLE:PATH="${LLVM_SOURCE_DIR}/utils/lit/lit.py"
                    -DFILECHECK_EXECUTABLE:PATH="$(build_directory_bin ${LOCAL_HOST} llvm)/FileCheck"
                    -DSWIFTC_EXECUTABLE:PATH=${SWIFTC_BIN}
                    -DFOUNDATION_BUILD_DIR:PATH="$(build_directory ${host} foundation)"
                    -DLIBDISPATCH_BUILD_DIR:PATH="$(build_directory ${host} libdispatch)"
                    -DLIBDISPATCH_SOURCE_DIR:PATH="${LIBDISPATCH_SOURCE_DIR}"

                    -Ddispatch_DIR:PATH=$(build_directory ${host} libdispatch)/cmake/modules
                    -DFoundation_DIR:PATH=$(build_directory ${host} foundation)/cmake/modules
                )

                # Ensure on Darwin platforms that we consider only the SQLite headers
                # from the SDK instead of picking ones found elsewhere
                # (e.g. in /usr/include )
                # Also consider only the SQLite dylib shipped with the OS
                # to avoid mismatch with the headers
                if [[ "$(uname -s)" = "Darwin" ]]; then
                    cmake_options=(
                        "${cmake_options[@]}"

                        -DSQLite3_INCLUDE_DIR:PATH="$(xcrun -sdk macosx -show-sdk-path)/usr/include"
                    )
                fi
                ;;
            xctest)
                XCTEST_BUILD_DIR=$(build_directory ${host} xctest)
                FOUNDATION_BUILD_DIR=$(build_directory ${host} foundation)
                SWIFT_BUILD_DIR=$(build_directory ${host} swift)

                case "${host}" in
                macosx-*)
                  # Staging: require opt-in for building with dispatch
                  if [[ ! "${SKIP_BUILD_LIBDISPATCH}" ]] ; then
                      LIBDISPATCH_BUILD_DIR="$(build_directory ${host} libdispatch)"
                      LIBDISPATCH_BUILD_ARGS="--libdispatch-src-dir=${LIBDISPATCH_SOURCE_DIR} --libdispatch-build-dir=${LIBDISPATCH_BUILD_DIR}"
                  fi

                  # Use XCTEST_BUILD_TYPE to build either --debug or --release.
                  if [[ "${XCTEST_BUILD_TYPE}" ==  "Debug" ]] ; then
                      XCTEST_BUILD_ARGS="--debug"
                  else
                      XCTEST_BUILD_ARGS="--release"
                  fi

                  call "${XCTEST_SOURCE_DIR}"/build_script.py \
                      --swiftc="${SWIFTC_BIN}" \
                      --build-dir="${XCTEST_BUILD_DIR}" \
                      --foundation-build-dir="${FOUNDATION_BUILD_DIR}" \
                      --swift-build-dir="${SWIFT_BUILD_DIR}" \
                      $LIBDISPATCH_BUILD_ARGS \
                      $XCTEST_BUILD_ARGS

                  # XCTest builds itself and doesn't rely on cmake
                  continue
                ;;
                *)
                  # FIXME: Always re-build XCTest on non-darwin platforms.
                  # The Swift project might have been changed, but CMake might
                  # not be aware and will not rebuild.
                  echo "Cleaning the XCTest build directory"
                  call rm -rf "${XCTEST_BUILD_DIR}"

                  cmake_options=(
                    ${cmake_options[@]}
                    -DCMAKE_BUILD_TYPE:STRING="${XCTEST_BUILD_TYPE}"
                    -DCMAKE_C_COMPILER:PATH="${CLANG_BIN}/clang"
                    -DCMAKE_CXX_COMPILER:PATH="${CLANG_BIN}/clang++"
                    -DCMAKE_Swift_COMPILER:PATH=${SWIFTC_BIN}
                    -DCMAKE_INSTALL_PREFIX:PATH="$(get_host_install_prefix ${host})"
                    -DCMAKE_INSTALL_LIBDIR:PATH="lib"

                    -Ddispatch_DIR=$(build_directory ${host} libdispatch)/cmake/modules
                    -DFoundation_DIR=$(build_directory ${host} foundation)/cmake/modules
                    -DLLVM_DIR=$(build_directory ${host} llvm)/lib/cmake/llvm

                    -DXCTEST_PATH_TO_LIBDISPATCH_SOURCE:PATH=${LIBDISPATCH_SOURCE_DIR}
                    -DXCTEST_PATH_TO_LIBDISPATCH_BUILD:PATH=$(build_directory ${host} libdispatch)
                    -DXCTEST_PATH_TO_FOUNDATION_BUILD:PATH=${FOUNDATION_BUILD_DIR}
                    -DCMAKE_SWIFT_COMPILER:PATH=${SWIFTC_BIN}
                    -DCMAKE_PREFIX_PATH:PATH=$(build_directory ${host} llvm)

                    -DENABLE_TESTING=YES
                  )
                ;;
                esac

                ;;
            foundation|foundation_static)
                # The configuration script requires knowing about XCTest's
                # location for building and running the tests. Note that XCTest
                # is not yet built at this point.
                XCTEST_BUILD_DIR=$(build_directory ${host} xctest)

                if [[ ${host} == "macosx"* ]]; then
                    echo "Skipping Foundation on OS X -- use the Xcode project instead"
                    continue
                fi

                if [[ ! "${SKIP_BUILD_LIBICU}" ]] ; then
                    ICU_ROOT=$(build_directory ${host} libicu)/tmp_install
                    ICU_LIBDIR="$(build_directory ${host} swift)/lib/swift/${SWIFT_HOST_VARIANT}/${SWIFT_HOST_VARIANT_ARCH}"
                    LIBICU_BUILD_ARGS=(
                        -DICU_ROOT:PATH=${ICU_ROOT}
                        -DICU_INCLUDE_DIR:PATH=${ICU_ROOT}/include
                        -DICU_UC_LIBRARIES:FILEPATH=${ICU_LIBDIR}/libicuucswift.so
                        -DICU_UC_LIBRARY:FILEPATH=${ICU_LIBDIR}/libicuucswift.so
                        -DICU_UC_LIBRARY_DEBUG:FILEPATH=${ICU_LIBDIR}/libicuucswift.so
                        -DICU_UC_LIBRARY_RELEASE:FILEPATH=${ICU_LIBDIR}/libicuucswift.so
                        -DICU_I18N_LIBRARIES:FILEPATH=${ICU_LIBDIR}/libicui18nswift.so
                        -DICU_I18N_LIBRARY:FILEPATH=${ICU_LIBDIR}/libicui18nswift.so
                        -DICU_I18N_LIBRARY_DEBUG:FILEPATH=${ICU_LIBDIR}/libicui18nswift.so
                        -DICU_I18N_LIBRARY_RELEASE:FILEPATH=${ICU_LIBDIR}/libicui18nswift.so
                    )
                else
                    LIBICU_BUILD_ARGS=()
                fi

                # FIXME: Always re-build XCTest on non-darwin platforms.
                # The Swift project might have been changed, but CMake might
                # not be aware and will not rebuild.
                echo "Cleaning the Foundation build directory"
                call rm -rf "${build_dir}"

                # Set the PKG_CONFIG_PATH so that core-foundation can find the libraries and
                # header files
                LIBICU_BUILD_DIR="$(build_directory ${host} libicu)"
                export PKG_CONFIG_PATH="${LIBICU_BUILD_DIR}/config:${PKG_CONFIG_PATH}"
                export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}":"${LIBICU_BUILD_DIR}/lib"

                cmake_options=(
                  ${cmake_options[@]}
                  -DCMAKE_BUILD_TYPE:STRING=${FOUNDATION_BUILD_TYPE}
                  -DCMAKE_C_COMPILER:PATH=${CLANG_BIN}/clang
                  -DCMAKE_CXX_COMPILER:PATH=${CLANG_BIN}/clang++
                  -DCMAKE_SWIFT_COMPILER:PATH=${SWIFTC_BIN}
                  -DCMAKE_Swift_COMPILER:PATH=${SWIFTC_BIN}
                  -DCMAKE_INSTALL_PREFIX:PATH=$(get_host_install_prefix ${host})

                  ${LIBICU_BUILD_ARGS[@]}

                  -DFOUNDATION_PATH_TO_LIBDISPATCH_SOURCE=${LIBDISPATCH_SOURCE_DIR}
                  -DFOUNDATION_PATH_TO_LIBDISPATCH_BUILD=$(build_directory ${host} libdispatch)
                  -Ddispatch_DIR=$(build_directory ${host} libdispatch)/cmake/modules

                  # NOTE(compnerd) we disable tests because XCTest is not ready
                  # yet, but we will reconfigure when the time comes.
                  -DENABLE_TESTING:BOOL=NO

                  -DBUILD_SHARED_LIBS=$([[ ${product} == foundation_static ]] && echo "NO" || echo "YES")
                )

                ;;
            libdispatch|libdispatch_static)
                LIBDISPATCH_BUILD_DIR=$(build_directory ${host} ${product})
                SWIFT_BUILD_PATH="$(build_directory ${host} swift)"

                case "${host}" in
                macosx-*)
                  echo "Error: build-script does not support building libdispatch on macOS?!"
                  usage 1>&2
                  exit 1
                ;;
                *)
                  # FIXME: Always re-build XCTest on non-darwin platforms.
                  # The Swift project might have been changed, but CMake might
                  # not be aware and will not rebuild.
                  echo "Cleaning the libdispatch build directory"
                  call rm -rf "${LIBDISPATCH_BUILD_DIR}"

                  cmake_options=(
                    -DENABLE_SWIFT=YES
                    ${cmake_options[@]}
                    -DCMAKE_BUILD_TYPE:STRING="${LIBDISPATCH_BUILD_TYPE}"
                    -DCMAKE_C_COMPILER:PATH="${CLANG_BIN}/clang"
                    -DCMAKE_CXX_COMPILER:PATH="${CLANG_BIN}/clang++"
                    -DCMAKE_SWIFT_COMPILER:PATH="${SWIFTC_BIN}"
                    -DCMAKE_Swift_COMPILER:PATH="${SWIFTC_BIN}"
                    -DCMAKE_INSTALL_PREFIX:PATH="$(get_host_install_prefix ${host})"
                    -DCMAKE_INSTALL_LIBDIR:PATH="lib"

                    -DSwift_DIR="${SWIFT_BUILD_PATH}/lib/cmake/swift"

                    -DENABLE_TESTING=YES
                    -DBUILD_SHARED_LIBS=$([[ ${product} == libdispatch_static ]] && echo "NO" || echo "YES")
                  )
                ;;
                esac

                ;;
            libicu)
                SWIFT_BUILD_PATH=$(build_directory ${host} swift)
                LIBICU_BUILD_DIR=$(build_directory ${host} ${product})
                ICU_TMPINSTALL=$LIBICU_BUILD_DIR/tmp_install
                ICU_TMPLIBDIR="${SWIFT_BUILD_PATH}/lib/swift/${SWIFT_HOST_VARIANT}/${SWIFT_HOST_VARIANT_ARCH}"
                if [[ "${RECONFIGURE}" || ! -f "${LIBICU_BUILD_DIR}"/config.status ]]; then
                    echo "Reconfiguring libicu"
                    if [[ "$LIBICU_BUILD_TYPE" != "Release" ]] ; then
                        libicu_enable_debug="--enable-debug"
                    else
                        libicu_enable_debug=""
                    fi
                    call mkdir -p "${LIBICU_BUILD_DIR}"

                    if [ $(true_false "${BUILD_SWIFT_STATIC_STDLIB}") == "TRUE" ]; then
                          libicu_enable_static="--enable-static"
                    else
                          libicu_enable_static=""
                    fi

                    with_pushd "${LIBICU_BUILD_DIR}" \
                        call env CXXFLAGS=-fPIC "${LIBICU_SOURCE_DIR}"/icu4c/source/runConfigureICU Linux \
                        ${icu_build_variant_arg} --prefix=${ICU_TMPINSTALL} \
                        ${libicu_enable_debug} \
                        --enable-renaming --with-library-suffix=swift \
                        --libdir=${ICU_TMPLIBDIR} \
                        --enable-shared --enable-static \
                        --enable-strict --disable-icuio \
                        --disable-plugins --disable-dyload --disable-extras \
                        --disable-samples --disable-layoutex --with-data-packaging=auto
                else
                    echo "Skipping reconfiguration of libicu"
                fi
                with_pushd "${LIBICU_BUILD_DIR}" \
                    call make -j ${BUILD_JOBS} install
                ICU_LIBDIR="$(build_directory ${host} swift)/lib/swift/${SWIFT_HOST_VARIANT}/${SWIFT_HOST_VARIANT_ARCH}"
                ICU_LIBDIR_STATIC="$(build_directory ${host} swift)/lib/swift_static/${SWIFT_HOST_VARIANT}"
                ICU_LIBDIR_STATIC_ARCH="$(build_directory ${host} swift)/lib/swift_static/${SWIFT_HOST_VARIANT}/${SWIFT_HOST_VARIANT_ARCH}"
                # Add in the ICU renaming config into uconfig.h
                call sed -e "/^#define __UCONFIG_H__/ r ${LIBICU_BUILD_DIR}/uconfig.h.prepend" -i ${ICU_TMPINSTALL}/include/unicode/uconfig.h

                if [ $(true_false "${BUILD_SWIFT_STATIC_STDLIB}") == "TRUE" ]; then
                    # Copy the static libs into the swift_static directory
                    call mkdir -p "${ICU_LIBDIR_STATIC_ARCH}"
                    for l in uc i18n data
                    do
                        lib="${ICU_LIBDIR}/libicu${l}swift.a"
                        call cp "${lib}" "${ICU_LIBDIR_STATIC}"
                        call cp "${lib}" "${ICU_LIBDIR_STATIC_ARCH}"
                    done
                fi

                # libicu builds itself and doesn't use cmake
                continue
                ;;
            *)
                echo "error: unknown product: ${product}"
                exit 1
                ;;
        esac

        # Compute the generator output file to check for, to determine if we
        # must reconfigure. We only handle Ninja for now.
        #
        # This is important for ensuring that if a CMake configuration fails in
        # CI, that we will still be willing to rerun the configuration process.
        generator_output_path=""
        if [[ "${CMAKE_GENERATOR}" == "Ninja" ]] ; then
            generator_output_path="${build_dir}/build.ninja"
        fi

        # Configure if necessary.
        cmake_cache_path="${build_dir}/CMakeCache.txt"
        if [[  "${RECONFIGURE}" || ! -f "${cmake_cache_path}" || \
                    ( ! -z "${generator_output_path}" && ! -f "${generator_output_path}" ) ]] ; then
            call mkdir -p "${build_dir}"

            # Use `cmake-file-api` in case it is available.
            call mkdir -p "${build_dir}/.cmake/api/v1/query"
            call touch "${build_dir}/.cmake/api/v1/query/codemodel-v2" "${build_dir}/.cmake/api/v1/query/cache-v2"

            if [[ -n "${DISTCC}" ]]; then
                EXTRA_DISTCC_OPTIONS=("DISTCC_HOSTS=localhost,lzo,cpp")
            fi
            with_pushd "${build_dir}" \
                call env "${EXTRA_DISTCC_OPTIONS[@]}" "${CMAKE}" "${cmake_options[@]}" "${EXTRA_CMAKE_OPTIONS[@]}" "${source_dir}"
        fi

        if [[ "${product}" == "libcxx" ]]; then
            continue
        fi

        # When we are building LLVM create symlinks to the c++ headers. We need
        # to do this before building LLVM since compiler-rt depends on being
        # built with the just built clang compiler. These are normally put into
        # place during the cmake step of LLVM's build when libcxx is in
        # tree... but we are not building llvm with libcxx in tree when we build
        # swift. So we need to do configure's work here.
        if [[ "${product}" == "llvm" ]]; then
            # Find the location of the c++ header dir.
            if [[ "$(uname -s)" == "Darwin" ]] ; then
              HOST_CXX_DIR=$(dirname "${HOST_CXX}")
              HOST_CXX_HEADERS_DIR="$HOST_CXX_DIR/../../usr/include/c++"
            elif [[ "$(uname -s)" == "Haiku" ]] ; then
              HOST_CXX_HEADERS_DIR="/boot/system/develop/headers/c++"
            elif [[ "${ANDROID_DATA}" ]] ; then
              HOST_CXX_HEADERS_DIR="$PREFIX/include/c++"
            else # Linux
              HOST_CXX_HEADERS_DIR="/usr/include/c++"
            fi

            # Find the path in which the local clang build is expecting to find
            # the c++ header files.
            BUILT_CXX_INCLUDE_DIR="$llvm_build_dir/include"

            echo "symlinking the system headers ($HOST_CXX_HEADERS_DIR) into the local clang build directory ($BUILT_CXX_INCLUDE_DIR)."
            call ln -s -f "$HOST_CXX_HEADERS_DIR" "$BUILT_CXX_INCLUDE_DIR"
        fi

        # Build.
        #
        # Even if builds are skipped, Swift configuration relies on
        # some LLVM tools like TableGen. In the LLVM configure rules
        # above, a small subset of LLVM build_targets are selected
        # when SKIP_BUILD is set.
        if [[ $(not ${SKIP_BUILD}) || "${product}" == "llvm" ]]; then
            if [[ "${CMAKE_GENERATOR}" == "Xcode" ]] ; then
                # Xcode generator uses "ALL_BUILD" instead of "all".
                # Also, xcodebuild uses -target instead of bare names.
                build_targets=("${build_targets[@]/all/ALL_BUILD}")
                build_targets=("${build_targets[@]/#/${BUILD_TARGET_FLAG} }")

                # Xcode can't restart itself if it turns out we need to reconfigure.
                # Do an advance build to handle that.
                call "${CMAKE_BUILD[@]}" "${build_dir}" $(cmake_config_opt ${product})
            fi

            call "${CMAKE_BUILD[@]}" "${build_dir}" $(cmake_config_opt ${product}) -- "${BUILD_ARGS[@]}" ${build_targets[@]}
        fi

        # When we are building LLVM copy over the compiler-rt
        # builtins for iOS/tvOS/watchOS to ensure that Swift's
        # stdlib can use compiler-rt builtins when targetting iOS/tvOS/watchOS.
        if [[ "${product}" = "llvm" ]] && [[ "${BUILD_LLVM}" = "1" ]] && [[ "$(uname -s)" = "Darwin" ]]; then
            HOST_CXX_DIR=$(dirname "${HOST_CXX}")
            HOST_LIB_CLANG_DIR="${HOST_CXX_DIR}/../lib/clang"
            DEST_LIB_CLANG_DIR="$(build_directory_bin ${host} llvm)/../lib/clang"

            if [[ -d "${HOST_LIB_CLANG_DIR}" ]] && [[ -d "${DEST_LIB_CLANG_DIR}" ]]; then
                DEST_CXX_BUILTINS_VERSION=$(ls "${DEST_LIB_CLANG_DIR}" | awk '{print $0}')
                DEST_BUILTINS_DIR="$(build_directory_bin ${host} llvm)/../lib/clang/$DEST_CXX_BUILTINS_VERSION/lib/darwin"

                if [[ -d "${DEST_BUILTINS_DIR}" ]]; then
                    for HOST_CXX_BUILTINS_PATH in "${HOST_LIB_CLANG_DIR}"/*; do
                        HOST_CXX_BUILTINS_DIR="${HOST_CXX_BUILTINS_PATH}/lib/darwin"
                        echo "copying compiler-rt embedded builtins from ${HOST_CXX_BUILTINS_DIR} into the local clang build directory ${DEST_BUILTINS_DIR}."

                        for OS in ios watchos tvos; do
                            # Copy over the device .a
                            LIB_NAME="libclang_rt.$OS.a"
                            HOST_LIB_PATH="$HOST_CXX_BUILTINS_DIR/$LIB_NAME"
                            if [[ -f "${HOST_LIB_PATH}" ]]; then
                                call cp "${HOST_LIB_PATH}" "${DEST_BUILTINS_DIR}/${LIB_NAME}"
                            elif [[ "${VERBOSE_BUILD}" ]]; then
                                echo "no file exists at ${HOST_LIB_PATH}"
                            fi
                            # Copy over the simulator .a
                            SIM_LIB_NAME="libclang_rt.${OS}sim.a"
                            HOST_SIM_LIB_PATH="$HOST_CXX_BUILTINS_DIR/$SIM_LIB_NAME"
                            if [[ -f "${HOST_SIM_LIB_PATH}" ]]; then
                                call cp "${HOST_SIM_LIB_PATH}" "${DEST_BUILTINS_DIR}/${SIM_LIB_NAME}"
                            elif [[ -f "${HOST_LIB_PATH}" ]]; then
                                # The simulator .a might not exist if the host
                                # Xcode is old. In that case, copy over the
                                # device library to the simulator location to allow
                                # clang to find it. The device library has the simulator
                                # slices in Xcode that doesn't have the simulator .a, so
                                # the link is still valid.
                                echo "copying over faux-sim library ${HOST_LIB_PATH} to ${SIM_LIB_NAME}"
                                call cp "${HOST_LIB_PATH}" "${DEST_BUILTINS_DIR}/${SIM_LIB_NAME}"
                            elif [[ "${VERBOSE_BUILD}" ]]; then
                                echo "no file exists at ${HOST_SIM_LIB_PATH}"
                            fi
                        done
                    done
                fi
            fi
        fi
    done
done
# END OF BUILD PHASE

# Trap function to print the current test configuration when tests fail.
# This is a function so the text is not unnecessarily displayed when running -x.
tests_busted ()
{
    echo "*** Failed while running tests for $1 $2"
}

for host in "${ALL_HOSTS[@]}"; do
    # Skip this pass when the only action to execute can't match.
    if ! [[ $(should_execute_host_actions_for_phase ${host} test) ]]; then
        continue
    fi

    # Calculate test targets
    calculate_targets_for_host $host

    set_build_options_for_host $host

    if [[ "${NATIVE_CLANG_TOOLS_PATH}" ]] ; then
        CLANG_BIN="${NATIVE_CLANG_TOOLS_PATH}"
    else
        CLANG_BIN="$(build_directory_bin ${LOCAL_HOST} llvm)"
    fi

    if [[ "${NATIVE_SWIFT_TOOLS_PATH}" ]] ; then
        SWIFTC_BIN="${NATIVE_SWIFT_TOOLS_PATH}/swiftc"
    else
        SWIFTC_BIN="$(build_directory_bin ${LOCAL_HOST} swift)/swiftc"
    fi

    # Run the tests for each product
    for product in "${PRODUCTS[@]}"; do
        # Check if we should perform this action.
        if ! [[ $(should_execute_action "${host}-${product}-test") ]]; then
            continue
        fi

        case ${product} in
            cmark)
                if [[ "${SKIP_TEST_CMARK}" ]]; then
                    continue
                fi
                executable_target=api_test
                results_targets=(test)
                if [[ "${CMAKE_GENERATOR}" == "Xcode" ]]; then
                    # Xcode generator uses "RUN_TESTS" instead of "test".
                    results_targets=(RUN_TESTS)
                fi
                ;;
            llvm)
                continue # We don't test LLVM
                ;;
            libcxx)
                continue # We don't test libc++
                ;;
            swift)
                executable_target=
                results_targets=
                if ! [[ "${SKIP_TEST_SWIFT}" ]]; then
                    if ! [[ $(is_cross_tools_host ${host}) ]] ; then
                       executable_target=SwiftUnitTests
                       results_targets=("${SWIFT_TEST_TARGETS[@]}")
                       if [[ "${STRESS_TEST_SOURCEKIT}" ]]; then
                          results_targets=(
                               "${results_targets[@]}"
                               stress-SourceKit
                          )
                       fi
                    fi
                fi
                if ! [[ "${SKIP_TEST_BENCHMARKS}" ]]; then
                    results_targets=(
                        "${results_targets[@]}"
                        "${SWIFT_RUN_BENCHMARK_TARGETS[@]}"
                    )
                fi
                if [[ -z "${results_targets[@]}" ]]; then
                    continue
                fi
                ;;
            lldb)
                if [[ "${SKIP_TEST_LLDB}" ]]; then
                    continue
                fi
                if [[ $(is_cross_tools_host ${host}) ]]; then
                    echo "--- Can't execute tests for ${host}, skipping... ---"
                    continue
                fi
                llvm_build_dir=$(build_directory ${host} llvm)
                lldb_build_dir=$(build_directory ${host} lldb)
                results_dir="${lldb_build_dir}/test-results"

                call mkdir -p "${results_dir}"
                LLVM_LIT_ARGS="${LLVM_LIT_ARGS} --xunit-xml-output=${results_dir}/results.xml"

                if [[ "${ENABLE_ASAN}" ]] ; then
                    # Limit the number of parallel tests
                    LLVM_LIT_ARGS="${LLVM_LIT_ARGS} -j $(sysctl hw.physicalcpu | awk -v N=${BUILD_JOBS} '{ print (N < $2) ? N : int($2/2) }')"
                fi

                FILTER_SWIFT_OPTION="--filter=[sS]wift"
                LLVM_LIT_FILTER_ARG=""
                if [[ "$(true_false ${LLDB_TEST_SWIFT_ONLY})" == "TRUE" ]]; then
                    LLVM_LIT_FILTER_ARG="${FILTER_SWIFT_OPTION}"
                fi

                # Record the times test took and report the slowest.
                LLVM_LIT_ARGS="${LLVM_LIT_ARGS} -v --time-tests"

                echo "--- Running LLDB unit tests ---"
                with_pushd ${lldb_build_dir} \
                    call ${NINJA_BIN} -j ${BUILD_JOBS} unittests/LLDBUnitTests
                echo "--- Running LLDB tests (Swift tests using ClangImporter) ---"
                with_pushd ${lldb_build_dir} \
                    call ${NINJA_BIN} -j ${BUILD_JOBS} lldb-test-deps
                with_pushd ${results_dir} \
                    call "${llvm_build_dir}/bin/llvm-lit" \
                         "${lldb_build_dir}/test" \
                         ${LLVM_LIT_ARGS} ${LLVM_LIT_FILTER_ARG}
                echo "--- Rerun LLDB Swift tests (using only DWARFImporter) ---"
                with_pushd ${results_dir} \
                    call "${llvm_build_dir}/bin/llvm-lit" \
                         "${lldb_build_dir}/test" \
                         ${LLVM_LIT_ARGS} ${FILTER_SWIFT_OPTION} \
                         --param dotest-args="--setting symbols.use-swift-clangimporter=false"

                if [[ -x "${LLDB_TEST_SWIFT_COMPATIBILITY}" ]] ; then
                    echo "Running LLDB swift compatibility tests against" \
                         "${LLDB_TEST_SWIFT_COMPATIBILITY}"
                    DOTEST_ARGS="-G swift-history --swift-compiler \"${LLDB_TEST_SWIFT_COMPATIBILITY}\""
                    with_pushd ${results_dir} \
                       call "${llvm_build_dir}/bin/llvm-lit" \
                            "${lldb_build_dir}/test" \
                            ${LLVM_LIT_ARGS} \
                            --param dotest-args="${DOTEST_ARGS}" \
                            --filter=compat
                fi
                continue
                ;;
            llbuild)
                if [[ "${SKIP_TEST_LLBUILD}" ]]; then
                    continue
                fi
                results_targets=("test")
                executable_target=""
                ;;
            xctest)
                if [[ "${SKIP_TEST_XCTEST}" ]]; then
                    continue
                fi

                case ${host} in
                macosx-*)
                  # If libdispatch is being built then XCTest will need access to it
                  if [[ ! "${SKIP_BUILD_LIBDISPATCH}" ]] ; then
                      LIBDISPATCH_BUILD_DIR="$(build_directory ${host} libdispatch)"
                      LIBDISPATCH_BUILD_ARGS="--libdispatch-src-dir=${LIBDISPATCH_SOURCE_DIR} --libdispatch-build-dir=${LIBDISPATCH_BUILD_DIR}"
                  fi

                  # Use XCTEST_BUILD_TYPE to build either --debug or --release.
                  if [[ "${XCTEST_BUILD_TYPE}" ==  "Debug" ]] ; then
                      XCTEST_BUILD_ARGS="--debug"
                  else
                      XCTEST_BUILD_ARGS="--release"
                  fi

                  echo "--- Running tests for ${product} ---"
                  FOUNDATION_BUILD_DIR=$(build_directory ${host} foundation)
                  XCTEST_BUILD_DIR=$(build_directory ${host} xctest)
                  call "${XCTEST_SOURCE_DIR}"/build_script.py test \
                      --swiftc="${SWIFTC_BIN}" \
                      --lit="${LLVM_SOURCE_DIR}/utils/lit/lit.py" \
                      --foundation-build-dir="${FOUNDATION_BUILD_DIR}" \
                      ${LIBDISPATCH_BUILD_ARGS} \
                      $XCTEST_BUILD_ARGS \
                      "${XCTEST_BUILD_DIR}"
                  echo "--- Finished tests for ${product} ---"
                  continue
                ;;
                *)
                  results_targets=( "check-xctest" )
                  executable_target=""
                ;;
                esac
                ;;
            foundation)
                # FIXME: Foundation doesn't build from the script on OS X
                if [[ ${host} == "macosx"* ]]; then
                    echo "Skipping Foundation on OS X -- use the Xcode project instead"
                    continue
                fi

                if [[ "${SKIP_TEST_FOUNDATION}" ]]; then
                    continue
                fi

                if [[ "${SKIP_BUILD_XCTEST}" ]]; then
                    continue
                fi

                if [[ ! "${SKIP_BUILD_LIBICU}" ]] ; then
                    ICU_ROOT=$(build_directory ${host} libicu)/tmp_install
                    ICU_LIBDIR="$(build_directory ${host} swift)/lib/swift/${SWIFT_HOST_VARIANT}/${SWIFT_HOST_VARIANT_ARCH}"
                    LIBICU_BUILD_ARGS=(
                        -DICU_ROOT:PATH=${ICU_ROOT}
                        -DICU_INCLUDE_DIR:PATH=${ICU_ROOT}/include
                        -DICU_UC_LIBRARIES:FILEPATH=${ICU_LIBDIR}/libicuucswift.so
                        -DICU_UC_LIBRARY:FILEPATH=${ICU_LIBDIR}/libicuucswift.so
                        -DICU_UC_LIBRARY_DEBUG:FILEPATH=${ICU_LIBDIR}/libicuucswift.so
                        -DICU_UC_LIBRARY_RELEASE:FILEPATH=${ICU_LIBDIR}/libicuucswift.so
                        -DICU_I18N_LIBRARIES:FILEPATH=${ICU_LIBDIR}/libicui18nswift.so
                        -DICU_I18N_LIBRARY:FILEPATH=${ICU_LIBDIR}/libicui18nswift.so
                        -DICU_I18N_LIBRARY_DEBUG:FILEPATH=${ICU_LIBDIR}/libicui18nswift.so
                        -DICU_I18N_LIBRARY_RELEASE:FILEPATH=${ICU_LIBDIR}/libicui18nswift.so
                    )
                else
                    LIBICU_BUILD_ARGS=()
                fi

                # NOTE(compnerd) the time has come to enable tests now
                cmake_options=(
                  ${cmake_options[@]}
                  -DCMAKE_BUILD_TYPE:STRING=${FOUNDATION_BUILD_TYPE}
                  -DCMAKE_C_COMPILER:PATH=${CLANG_BIN}/clang
                  -DCMAKE_CXX_COMPILER:PATH=${CLANG_BIN}/clang++
                  -DCMAKE_Swift_COMPILER:PATH=${SWIFTC_BIN}
                  -DCMAKE_INSTALL_PREFIX:PATH=$(get_host_install_prefix ${host})

                  ${LIBICU_BUILD_ARGS[@]}

                  -DFOUNDATION_PATH_TO_LIBDISPATCH_SOURCE=${LIBDISPATCH_SOURCE_DIR}
                  -DFOUNDATION_PATH_TO_LIBDISPATCH_BUILD=$(build_directory ${host} libdispatch)
                  -Ddispatch_DIR=$(build_directory ${host} libdispatch)/cmake/modules

                  -DENABLE_TESTING:BOOL=YES
                  -DXCTest_DIR=$(build_directory ${host} xctest)/cmake/modules

                  -DCMAKE_SWIFT_COMPILER:PATH=${SWIFTC_BIN}
                  -DFOUNDATION_PATH_TO_XCTEST_BUILD:PATH=$(build_directory ${host} xctest)
                )

                [[ -z "${DISTCC}" ]] || EXTRA_DISTCC_OPTIONS=("DISTCC_HOSTS=localhost,lzo,cpp")
                export CTEST_OUTPUT_ON_FAILURE=1
                with_pushd "$(build_directory ${host} foundation)" \
                    call env "${EXTRA_DISTCC_OPTIONS[@]}" "${CMAKE}" "${cmake_options[@]}" "${EXTRA_CMAKE_OPTIONS[@]}" "${FOUNDATION_SOURCE_DIR}"

                results_targets=( "test" )
                executable_target=("TestFoundation")
                ;;
            foundation_static)
              continue
            ;;
            libdispatch)
                if [[ "${SKIP_TEST_LIBDISPATCH}" ]]; then
                    continue
                fi

                case "${host}" in
                macosx-*)
                  LIBDISPATCH_BUILD_DIR=$(build_directory ${host} ${product})
                  echo "--- Running tests for ${product} ---"
                  with_pushd "${LIBDISPATCH_BUILD_DIR}" \
                      call env VERBOSE=1 make check
                  echo "--- Finished tests for ${product} ---"
                  continue
                ;;
                *)
                  results_targets=( "test" )
                  executable_target=""
                ;;
                esac
                ;;
            libdispatch_static)
              # FIXME: merge with libdispatch once the unit tests work with
              # libdispatch_static
              continue
            ;;
            libicu)
                if [[ "${SKIP_TEST_LIBICU}" ]]; then
                    continue
                fi
                LIBICU_BUILD_DIR=$(build_directory ${host} ${product})
                echo "--- Running tests for ${product} ---"
                with_pushd "${LIBICU_BUILD_DIR}/test" \
                    call make -j ${BUILD_JOBS}
                echo "--- Finished tests for ${product} ---"
                continue
                ;;
            *)
                echo "error: unknown product: ${product}"
                exit 1
                ;;
        esac

        trap "tests_busted ${product} ''" ERR
        build_dir=$(build_directory ${host} ${product})
        build_cmd=("${CMAKE_BUILD[@]}" "${build_dir}" $(cmake_config_opt ${product}) -- "${BUILD_ARGS[@]}")

        if [[ "${executable_target}" != "" ]]; then
            echo "--- Building tests for ${product} ---"
            call "${build_cmd[@]}" ${BUILD_TARGET_FLAG} "${executable_target}"
        fi

        # We can only run tests built for the host machine, because
        # cross-compiled hosts only build their native target. See: get_stdlib_targets_for_host()
        if [[ $(is_cross_tools_host ${host}) ]]; then
            echo "--- Can't execute tests for ${host}, skipping... ---"
            continue
        fi

        echo "--- Running tests for ${product} ---"
        for target in "${results_targets[@]}"; do
            if [[ "${target}" != "" ]]; then
                echo "--- ${target} ---"
                trap "tests_busted ${product} '(${target})'" ERR

                test_target="$target"
                if [[ ${test_target} == check-swift* ]] && [[ "${TEST_PATHS}" ]]; then
                    test_target="${test_target}-custom"
                fi

                call "${build_cmd[@]}" ${BUILD_TARGET_FLAG} ${test_target}

                echo "-- ${target} finished --"
            fi
        done

        trap - ERR
        echo "--- Finished tests for ${product} ---"
    done
done
# END OF TEST PHASE


LIPO_SRC_DIRS=()

for host in "${ALL_HOSTS[@]}"; do
    # Calculate the directory to install products in to.
    host_install_destdir=$(get_host_install_destdir ${host})
    host_install_prefix=$(get_host_install_prefix ${host})

    # Identify the destdirs to pass to lipo. Must happen even if the install action
    # is to be skipped.
    if [[ $(should_include_host_in_lipo ${host}) ]]; then
        LIPO_SRC_DIRS+=( "${host_install_destdir}" )
    fi

    # Skip this pass when the only action to execute can't match.
    if ! [[ $(should_execute_host_actions_for_phase ${host} install) ]]; then
        continue
    fi

    # Skip this pass if flag is set and we are cross compiling and it's the local host.
    if [[ "${SKIP_LOCAL_HOST_INSTALL}" ]] && [[ $(has_cross_compile_hosts) ]] && [[ ${host} == ${LOCAL_HOST} ]]; then
        continue
    fi

    # Set the build options for this host
    set_build_options_for_host $host

    for product in "${PRODUCTS[@]}"; do
        [[ $(should_execute_action "${host}-${product/_static}-install") ]] || continue
        if [[ -z "${INSTALL_DESTDIR}" ]] ; then
            echo "--install-destdir is required to install products."
            exit 1
        fi

        INSTALL_TARGETS="install"

        case ${product} in
            cmark)
                if [[ -z "${INSTALL_CMARK}" ]] ; then
                    continue
                fi
                ;;
            llvm)
                if [[ -z "${INSTALL_LLVM}" ]] ; then
                    continue
                fi

                if [[ "${LLVM_INSTALL_COMPONENTS}" == "all" ]] ; then
                    INSTALL_TARGETS=install
                elif [[ -n "${LLVM_INSTALL_COMPONENTS}" ]] ; then
                    if [[ $(is_cross_tools_host ${host}) && "${LLVM_INSTALL_COMPONENTS}" == *"compiler-rt"* ]]; then
                        INSTALL_TARGETS=install-$(echo ${LLVM_INSTALL_COMPONENTS} | sed -E 's/compiler-rt;//g' |sed -E 's/;/ install-/g')
                    else
                        INSTALL_TARGETS=install-$(echo ${LLVM_INSTALL_COMPONENTS} | sed -E 's/;/ install-/g')
                    fi
                fi
                ;;
            libcxx)
                if [[ -z "${INSTALL_LIBCXX}" ]] ; then
                    continue
                fi
                INSTALL_TARGETS=install-cxx-headers
                ;;
            swift)
                if [[ -z "${INSTALL_SWIFT}" ]] ; then
                    continue
                fi
                INSTALL_TARGETS=install-swift-components
                ;;
            llbuild)
                if [[ -z "${INSTALL_LLBUILD}" ]] ; then
                    continue
                fi
                INSTALL_TARGETS="install-swift-build-tool install-libllbuildSwift"
                ;;
            # Products from this here install themselves; they don't fall-through.
            lldb)
                if [[ -z "${INSTALL_LLDB}" ]] ; then
                    continue
                fi
                INSTALL_TARGETS="install-distribution"
                ;;
            xctest)
                if [[ -z "${INSTALL_XCTEST}" ]] ; then
                    continue
                fi

                case ${host} in
                  linux-*|freebsd-*|cygwin-*|haiku-*|android-*) ;;
                  *)
                    echo "error: --install-xctest is not supported on this platform"
                    exit 1
                  ;;
                esac

                ;;
            foundation|foundation_static)
                # FIXME: Foundation doesn't build from the script on OS X
                if [[ ${host} == "macosx"* ]]; then
                    echo "Skipping Foundation on OS X -- use the Xcode project instead"
                    continue
                fi

                if [[ -z "${INSTALL_FOUNDATION}" ]] ; then
                    continue
                fi

                ;;
            libdispatch|libdispatch_static)
                if [[ -z "${INSTALL_LIBDISPATCH}" ]] ; then
                    continue
                fi

                case "${host}" in
                macosx-*)
                  if [[ -z "${INSTALL_DESTDIR}" ]] ; then
                      echo "--install-destdir is required to install products."
                      exit 1
                  fi
                  echo "--- Installing ${product} ---"
                  LIBDISPATCH_BUILD_DIR=$(build_directory ${host} ${product})
                  with_pushd "${LIBDISPATCH_BUILD_DIR}" \
                      call make install
                  DISPATCH_LIBDIR="${host_install_destdir}${host_install_prefix}/lib/swift/${SWIFT_HOST_VARIANT}"
                  DISPATCH_LIBDIR_STATIC="${host_install_destdir}${host_install_prefix}/lib/swift_static/${SWIFT_HOST_VARIANT}"
                  if [ -f "$DISPATCH_LIBDIR/libdispatch.a" ]; then
                      mv "$DISPATCH_LIBDIR/libdispatch.a" "$DISPATCH_LIBDIR_STATIC"
                  fi

                  # As libdispatch installation is self-contained, we break early here.
                  continue
                ;;
                *)
                ;;
                esac
                ;;
            libicu)
                if [[ -z "${INSTALL_LIBICU}" ]]; then
                    continue
                fi
                echo "--- Installing ${product} ---"
                ICU_BUILD_DIR=$(build_directory ${host} ${product})
                ICU_INSTALL_DIR="$(get_host_install_destdir ${host})$(get_host_install_prefix ${host})"
                ICU_LIBDIR="$(build_directory ${host} swift)/lib/swift/${SWIFT_HOST_VARIANT}/${SWIFT_HOST_VARIANT_ARCH}"
                LIBICU_DEST_DIR="${ICU_INSTALL_DIR}lib/swift/${SWIFT_HOST_VARIANT}"
                call mkdir -p ${LIBICU_DEST_DIR}

                for l in uc i18n data
                do
                    lib=${ICU_LIBDIR}/libicu${l}swift
                    echo "${lib} => ${LIBICU_DEST_DIR}"
                    call cp -d ${lib}.so ${lib}.so.* ${LIBICU_DEST_DIR}
                done

                if [ $(true_false "${BUILD_SWIFT_STATIC_STDLIB}") == "TRUE" ]; then
                    LIBICU_DEST_DIR_STATIC="${ICU_INSTALL_DIR}lib/swift_static/${SWIFT_HOST_VARIANT}"
                    call mkdir -p ${LIBICU_DEST_DIR_STATIC}
                    for l in uc i18n data
                    do
                        lib=${ICU_LIBDIR}/libicu${l}swift
                        echo "${lib} => ${LIBICU_DEST_DIR_STATIC}"
                        call cp -d ${lib}.a ${LIBICU_DEST_DIR_STATIC}
                    done
                fi

                ICU_TMP_INSTALL_DIR="${ICU_BUILD_DIR}/tmp_install"
                call mkdir -p  "${ICU_INSTALL_DIR}include"
                call cp -a "${ICU_TMP_INSTALL_DIR}/include/unicode" "${ICU_INSTALL_DIR}include"
                call mkdir -p "${ICU_INSTALL_DIR}share/icuswift"
                call cp -a "${ICU_TMP_INSTALL_DIR}/share/icuswift" "${ICU_INSTALL_DIR}share"
                continue
                ;;
            *)
                echo "error: unknown product: ${product}"
                exit 1
                ;;
        esac

        echo "--- Installing ${product} ---"
        build_dir=$(build_directory ${host} ${product})

        call env DESTDIR="${host_install_destdir}" "${CMAKE_BUILD[@]}" "${build_dir}" -- ${INSTALL_TARGETS}
    done
done

for host in "${ALL_HOSTS[@]}"; do
    # Check if we should perform this action.
    if ! [[ $(should_execute_action "${host}-extractsymbols") ]]; then
        continue
    fi

    # Skip this pass if flag is set and we are cross compiling and it's the local host.
    if [[ "${SKIP_LOCAL_HOST_INSTALL}" ]] && [[ $(has_cross_compile_hosts) ]] && [[ ${host} == ${LOCAL_HOST} ]]; then
        continue
    fi

    # Calculate the directory to install products in to.
    host_install_destdir=$(get_host_install_destdir ${host})
    host_install_prefix=$(get_host_install_prefix ${host})

    if [[ "${DARWIN_INSTALL_EXTRACT_SYMBOLS}" ]] && [[ $(host_has_darwin_symbols ${host}) ]]; then
        echo "--- Extracting symbols ---"

        # The usage for this script says that lipo happens before
        # dsym extraction but that's not really what happens.  At this point,
        # we're processing an individual host (eg macosx-x86_64) but targeting
        # the (shared) SYMROOT which can cause mutliple hosts to stomp on each
        # other.  As a hack, I'm segregating the hosts in the symroot but it
        # would probably be better to make the script behave as the usage
        # descibes
        host_symroot="${INSTALL_SYMROOT}/${host}"

        # FIXME: Since it's hard to trace output pipe call,
        #        For now, We don't support dry-run trace for this block
        #        Instead, just echo we do "darwin_intall_extract_symbols".
        if [[ "${DRY_RUN}" ]]; then
            call darwin_install_extract_symbols
        else
            set -x

            CURRENT_INSTALL_DIR=${host_install_destdir}
            CURRENT_PREFIX="${TOOLCHAIN_PREFIX}"

            # Copy executables and shared libraries from the `host_install_destdir` to
            # INSTALL_SYMROOT and run dsymutil on them.
            (cd "${CURRENT_INSTALL_DIR}" &&
             find ./"${CURRENT_PREFIX}" -perm -0111 -type f -print | cpio --insecure -pdm "${host_symroot}")

            dsymutil_path=
            if [[ -n "${DARWIN_INSTALL_EXTRACT_SYMBOLS_USE_JUST_BUILT_DSYMUTIL}" ]]; then
                dsymutil_path=$(find_just_built_local_host_llvm_tool dsymutil)
            else
                dsymutil_path=$(xcrun_find_tool dsymutil)
            fi

            # Run dsymutil on executables and shared libraries.
            #
            # Exclude shell scripts and static archives.
            # Exclude swift-api-digester dSYM to reduce debug toolchain size.
            (cd "${host_symroot}" &&
             find ./"${CURRENT_PREFIX}" -perm -0111 -type f -print | \
               grep -v '.py$' | \
               grep -v '.a$' | \
               grep -v 'swift-api-digester' | \
               xargs -n 1 -P ${BUILD_JOBS} ${dsymutil_path})

            # Strip executables, shared libraries and static libraries in
            # `host_install_destdir`.
            find "${CURRENT_INSTALL_DIR}${CURRENT_PREFIX}/" \
              '(' -perm -0111 -or -name "*.a" ')' -type f -print | \
              xargs -n 1 -P ${BUILD_JOBS} $(xcrun_find_tool strip) -S

            # Codesign dylibs after strip tool
            # rdar://45388785
            find "${CURRENT_INSTALL_DIR}${CURRENT_PREFIX}/" \
              '(' -name "*.dylib" ')' -type f -print | \
              xargs -n 1 -P ${BUILD_JOBS} $(xcrun_find_tool codesign) -f -s -

            { set +x; } 2>/dev/null
        fi
    fi
done
# Everything is 'installed', but some products may be awaiting lipo.


function build_and_test_installable_package() {

    local host="$1"

    if [[ "${INSTALLABLE_PACKAGE}" ]] ; then

        # Get the directory where the products where installed.
        # If INSTALL_DESTDIR not given, we couldn't have installed anything.

        if [[ -z "${INSTALL_DESTDIR}" ]] ; then
            echo "--install-destdir required to build a package. Skipping."
            return
        fi
        local host_install_destdir="$(get_host_install_destdir ${host})"
        local host_install_prefix="$(get_host_install_prefix ${host})"

        if [[ $(has_cross_compile_hosts) ]]; then
            package_for_host="${INSTALLABLE_PACKAGE}-${host}"
        else
            package_for_host="${INSTALLABLE_PACKAGE}"
        fi

        echo "--- Creating installable package ---"
        echo "-- Package file: ${package_for_host} --"

        # Assume the lipo builds are (or include) an OS X host and build an xctoolchain
        if [[ "${host}" == "macosx-"* ]] || [[ "${host}" == "merged-hosts" ]]; then
          # Create plist for xctoolchain.
          echo "-- Create Info.plist --"
          PLISTBUDDY_BIN="/usr/libexec/PlistBuddy"

          DARWIN_TOOLCHAIN_INSTALL_LOCATION="/Library/Developer/Toolchains/${DARWIN_TOOLCHAIN_NAME}.xctoolchain"
          DARWIN_TOOLCHAIN_INFO_PLIST="${host_install_destdir}${TOOLCHAIN_PREFIX}/Info.plist"
          DARWIN_TOOLCHAIN_REPORT_URL="https://bugs.swift.org/"
          COMPATIBILITY_VERSION=2
          COMPATIBILITY_VERSION_DISPLAY_STRING="Xcode 8.0"
          DARWIN_TOOLCHAIN_CREATED_DATE="$(date -u +'%a %b %d %T GMT %Y')"

          echo "-- Removing: ${DARWIN_TOOLCHAIN_INFO_PLIST}"
          call rm -f ${DARWIN_TOOLCHAIN_INFO_PLIST}

          call ${PLISTBUDDY_BIN} -c "Add DisplayName string '${DARWIN_TOOLCHAIN_DISPLAY_NAME}'" "${DARWIN_TOOLCHAIN_INFO_PLIST}"
          call ${PLISTBUDDY_BIN} -c "Add ShortDisplayName string '${DARWIN_TOOLCHAIN_DISPLAY_NAME_SHORT}'" "${DARWIN_TOOLCHAIN_INFO_PLIST}"
          call ${PLISTBUDDY_BIN} -c "Add CreatedDate date '${DARWIN_TOOLCHAIN_CREATED_DATE}'" "${DARWIN_TOOLCHAIN_INFO_PLIST}"
          call ${PLISTBUDDY_BIN} -c "Add CompatibilityVersion integer ${COMPATIBILITY_VERSION}" "${DARWIN_TOOLCHAIN_INFO_PLIST}"
          call ${PLISTBUDDY_BIN} -c "Add CompatibilityVersionDisplayString string ${COMPATIBILITY_VERSION_DISPLAY_STRING}" "${DARWIN_TOOLCHAIN_INFO_PLIST}"
          call ${PLISTBUDDY_BIN} -c "Add Version string '${DARWIN_TOOLCHAIN_VERSION}'" "${DARWIN_TOOLCHAIN_INFO_PLIST}"
          call ${PLISTBUDDY_BIN} -c "Add CFBundleIdentifier string '${DARWIN_TOOLCHAIN_BUNDLE_IDENTIFIER}'" "${DARWIN_TOOLCHAIN_INFO_PLIST}"
          call ${PLISTBUDDY_BIN} -c "Add ReportProblemURL string '${DARWIN_TOOLCHAIN_REPORT_URL}'" "${DARWIN_TOOLCHAIN_INFO_PLIST}"
          call ${PLISTBUDDY_BIN} -c "Add Aliases array" "${DARWIN_TOOLCHAIN_INFO_PLIST}"
          call ${PLISTBUDDY_BIN} -c "Add Aliases:0 string '${DARWIN_TOOLCHAIN_ALIAS}'" "${DARWIN_TOOLCHAIN_INFO_PLIST}"
          call ${PLISTBUDDY_BIN} -c "Add OverrideBuildSettings dict" "${DARWIN_TOOLCHAIN_INFO_PLIST}"
          call ${PLISTBUDDY_BIN} -c "Add OverrideBuildSettings:ENABLE_BITCODE string 'NO'" "${DARWIN_TOOLCHAIN_INFO_PLIST}"
          call ${PLISTBUDDY_BIN} -c "Add OverrideBuildSettings:SWIFT_DISABLE_REQUIRED_ARCLITE string 'YES'" "${DARWIN_TOOLCHAIN_INFO_PLIST}"
          call ${PLISTBUDDY_BIN} -c "Add OverrideBuildSettings:SWIFT_LINK_OBJC_RUNTIME string 'YES'" "${DARWIN_TOOLCHAIN_INFO_PLIST}"
          call ${PLISTBUDDY_BIN} -c "Add OverrideBuildSettings:SWIFT_DEVELOPMENT_TOOLCHAIN string 'YES'" "${DARWIN_TOOLCHAIN_INFO_PLIST}"
          call ${PLISTBUDDY_BIN} -c "Add OverrideBuildSettings:SWIFT_USE_DEVELOPMENT_TOOLCHAIN_RUNTIME string 'YES'" "${DARWIN_TOOLCHAIN_INFO_PLIST}"

          call chmod a+r "${DARWIN_TOOLCHAIN_INFO_PLIST}"

          if [[ "${DARWIN_TOOLCHAIN_APPLICATION_CERT}" ]] ; then
            echo "-- Codesign xctoolchain --"
            call "${SWIFT_SOURCE_DIR}/utils/toolchain-codesign" "${DARWIN_TOOLCHAIN_APPLICATION_CERT}" "${host_install_destdir}${TOOLCHAIN_PREFIX}/"
          fi
          if [[ "${DARWIN_TOOLCHAIN_INSTALLER_PACKAGE}" ]] ; then
            echo "-- Create Installer --"
            call "${SWIFT_SOURCE_DIR}/utils/toolchain-installer" "${host_install_destdir}${TOOLCHAIN_PREFIX}/" "${DARWIN_TOOLCHAIN_BUNDLE_IDENTIFIER}" \
                "${DARWIN_TOOLCHAIN_INSTALLER_CERT}" "${DARWIN_TOOLCHAIN_INSTALLER_PACKAGE}" "${DARWIN_TOOLCHAIN_INSTALL_LOCATION}" \
                "${DARWIN_TOOLCHAIN_VERSION}" "${SWIFT_SOURCE_DIR}/utils/darwin-installer-scripts"
          fi

          # host_install_destdir contains the toolchain prefix.
          # We want to create the package in host_install_destdir_nonprefixed.
          with_pushd "${host_install_destdir}" \
              call tar -c -z -f "${package_for_host}" "${TOOLCHAIN_PREFIX/#\/}"
        else
            # BSD tar doesn't support --owner/--group.
            if [[ "$(uname -s)" == "Darwin" || "$(uname -s)" == "FreeBSD" ]] ; then
                with_pushd "${host_install_destdir}" \
                    tar -c -z -f "${package_for_host}" "${host_install_prefix/#\/}"
            else
                with_pushd "${host_install_destdir}" \
                    tar -c -z -f "${package_for_host}" --owner=0 --group=0 "${host_install_prefix/#\/}"
            fi
        fi
        if [[ "${TEST_INSTALLABLE_PACKAGE}" ]] ; then
            PKG_TESTS_SOURCE_DIR="${WORKSPACE}/swift-integration-tests"
            PKG_TESTS_SANDBOX_PARENT="$(build_directory swift_package_sandbox_${host} none)"
            PKG_TESTS_TEMPS="${PKG_TESTS_SANDBOX_PARENT}"/"tests"

            if [[ "${host}" == "macosx-"* ]] ; then
                PKG_TESTS_SANDBOX="${PKG_TESTS_SANDBOX_PARENT}"/"${TOOLCHAIN_PREFIX}"
            else # Linux
                PKG_TESTS_SANDBOX="${PKG_TESTS_SANDBOX_PARENT}"
            fi

            LIT_EXECUTABLE_PATH="${LLVM_SOURCE_DIR}/utils/lit/lit.py"
            LLVM_BIN_DIR="$(build_directory_bin ${LOCAL_HOST} llvm)"
            echo "-- Test Installable Package --"
            call rm -rf "${PKG_TESTS_SANDBOX_PARENT}"
            call mkdir -p "${PKG_TESTS_SANDBOX}"
            with_pushd "${PKG_TESTS_SANDBOX_PARENT}" \
                call tar xzf "${package_for_host}"

            with_pushd "${PKG_TESTS_SOURCE_DIR}" \
                call python "${LIT_EXECUTABLE_PATH}" . -sv --param package-path="${PKG_TESTS_SANDBOX}" --param test-exec-root="${PKG_TESTS_TEMPS}" --param llvm-bin-dir="${LLVM_BIN_DIR}"
        fi
    fi
}

# Build and test packages.
for host in "${ALL_HOSTS[@]}"; do

    # Check if we should perform this action.
    if ! [[ $(should_execute_action "${host}-package") ]]; then
        continue
    fi

    if [[ $(should_include_host_in_lipo ${host}) ]]; then
            continue
    fi

    build_and_test_installable_package ${host}
done

# Lipo those products which require it, optionally build and test an installable package.
mergedHost="merged-hosts"
if [[ ${#LIPO_SRC_DIRS[@]} -gt 0 ]]; then
    # This is from multiple hosts; Which host should we say it is?
    # Let's call it 'merged-hosts' so that we can identify it.

    if [[ $(should_execute_action "${mergedHost}-lipo") ]]; then
        # Allow passing lipo with --host-lipo
        if [[ -z "${HOST_LIPO}" ]] ; then
            LIPO_PATH=$(xcrun_find_tool lipo)
        else
            LIPO_PATH="${HOST_LIPO}"
        fi
        call "${SWIFT_SOURCE_DIR}"/utils/recursive-lipo --lipo=${LIPO_PATH} --copy-subdirs="$(get_host_install_prefix ${host})lib/swift $(get_host_install_prefix ${host})lib/swift_static" --destination="$(get_host_install_destdir ${mergedHost})" ${LIPO_SRC_DIRS[@]}

        # Build and test the lipo-ed package.
        build_and_test_installable_package ${mergedHost}
    fi
fi
# END
